Basic Data Visualization using python

Saisurya Anbazhagan Vanniyar
4 min readFeb 7, 2021

Visualizing data in python is not as hard as you think, follow this article to know more about it. This post will teach you basic data visualization in python using matplotlib library. Following topics will be discussed in this article.

  1. Line Chart
  2. Bar Chart
  3. Histogram
  4. Pie Chart
  5. Scatter Plot
  6. Box plot
  1. Line Chart

Line chart or line plots is used to display information as series of data. In python we can plot several line charts in single graph to see how they are related with each other. Consider the following code:

  • temp is list containing temperatures (in °C) for a week.
  • wind is list containing wind speed (in km/hr) for a week.
  • plt.axes() is used to plot axes for the graph.
  • set_xlabel() and set_ylabel() are used to label the respective axes.
  • plt.plot() is used to plot the line graph, here ‘o-’ specifies type of line and color is used to specify the color of the line. You can use this to plot as many line plots as you wish.
  • plt.legend() is used to display summary of the graph which is shown in the output at upper right corner.
  • plt.savefig () is used to save the image in your computer. You can save the image in any format you want(png, jpeg, tiff etc).
  • plt.show() is used to display the output.

Following is the output produced.

2. Bar Graph

Bar charts are used represent to categorical data. Consider the following code:

  • temp is list containing temperatures (in °C) for a week.
  • days is list containing week days.
  • b and g are just variables to store colors.
  • colors is list containing color values for each bar (length of the colors list must be same as length of the data that you wish to display).
  • plt.bar() is used to plot bar graph where you specify the values to be plotted with desired colors and widths.
  • plt.savefig () and plt.show() are same as above code

3. Histograms

Histograms are used to represent the approximate frequency distribution of the data. It is done by classifying the data in ‘bins’ for better visualization. Consider the following code. The area of the bins represent it’s frequency and width represent the class interval. Consider following example:

  • temp is list containing temperatures (in °C) for a month.
  • plt.hist() is used to plot histograms where you specify the values to be plotted with desired colors. The bins is used to specify class length.
  • plt.savefig () and plt.show() serve the function of saving and displaying results.

4. Pie Chart

Pie charts are used to visualize parts of a whole. The size of sector represents it’s proportion. Consider following code:

  • weather is list that specifies the weather type.
  • colors is a list that specifies colors for particular label(here weather).
  • explode is a tuple that is used to show a slice separated from the pie chart.
  • days is a list that represents the number of days it was a particular weather.
  • plt.pie() is used to plot pie chart.
  • plt.legend() shows summary of data(as shown in the figure in upper left corner).
  • plt.savefig () and plt.show() serve the function of saving and displaying results.

5. Scatter Plot

Scatter plots visualize data as clusters. They are used to see data patterns which proves quite useful in data analysis process. Consider following code:

  • temp is list containing temperatures (in °C) for a month..
  • rain is a list to specify amount of rain(in mm) for a particular day.
  • plt.scatter() is used to plot scatter plot. cmap is used to specify the inbuilt color maps.
  • plt.legend() shows summary of data(as shown in the figure in upper left corner).
  • colorbar() provides separate axis to specify color and it’s range.
  • plt.savefig () and plt.show() serve the function of saving and displaying results.

6. Box Plot

Box plot is used to represent numerical data through their quartiles. It provides summary of data (such as minimum, maximum, median) in graphical form. Consider the following code:

  • temp is list containing temperatures (in °C) for a month..
  • precipitation is a list to specify precipitation(in %) for a particular day.
  • set_xtickslabels() is used to specify the data that is being plotted.
  • plt.boxplot() is used to plot boxplot. You can plot as many boxplot you want.
  • plt.savefig () and plt.show() serve the function of saving and displaying results.

If you liked this article show your appreciation by commenting or clapping or both!

--

--

Saisurya Anbazhagan Vanniyar

I am a curious programmer who likes to develop programs and applications and share my knowledge with others.