Making Simple Graphs with Matplotlib

Make Neat Visualizations with Few Lines of Code

Photo by Markus Winkler on Unsplash

Matplotlib is no stranger to the world of Data Visualization. It is one of the most popular libraries used to visualize arrays of data in Python. In short, matplotlibwill help you create and plot graphs and perform operations simultaneously for better and intuitive presentation.

Let us understand this plot by plot and graph by graph 😊.

Bar graphs are the common types of plots when you need to measure numerical data across categories. Here is a sample visualization followed by the code for the same. The sample data is composed of random sales numbers and metadata. The fake dataset consists of the following:

  • Sales — Numeric
  • Products — Smartphones, Television, Laptops, Accessories and Batteries

The sub-package pyplot is used to build plots and graphs in matplotlib. To begin with, I always create a NumPy array of all the facts and dimensions.

  • We use plt.bar because it is a bar graph
  • plt.show is used to visualize the plot

The graph looks simple. It gives us a clear idea of sales across different product categories. Let’s improve upon this visualization with the following aspects.

  • Aligning it to the center
  • Changing the color and bar width
  • Giving a title
  • Labeling the axis
  • Modifying the axis

You can specify labels for both, x and y-axis using plt.label. In the above illustration, I have modified the ticks in the y axis in the following format; Range = (Start, Stop, Interval), where each representative label is divided by hundred thousand. You can customize ticks for both the axis.

The fontdict assists in changing the font size, weight and color. Feel free to experiment with the entire set of ‘RGB’ colors in Matplotlib.

Scatter plot is a type of chart that displays a relationship between two variables. It is used extensively to determine the correlation between two quantitative variables and highlight outliers.

Let’s begin with the Fish dataset that consists of:

  • Species — Bream, Parkki, Whitefish, Perch, Pike, Roach and Smelt
  • Weight
  • Length — Length of different fins
  • Height
  • Width

Creating NumPy arrays for the above facts and dimensions.

We use plt.scatter because it is a scatter plot. We can use the same code as mentioned in the bar graph above. The alpha denotes opacity and s indicates the size of the individual data points. Let’s upgrade this visualization with the following aspects:

  • Plot by each species
  • Adding the legend
  • You can also use plt.annotate to highlight the bubbles
  • Understand the function of s

Always make your plots and graphs more intuitive and interactive. The purpose is not just limited to visual illustration but to derive relationships between data points. In addition, you should experiment with box and whisker plots, histograms, line graphs and more!

Check out the entire code on GitHub: https://github.com/kashamcrash/MattyPlotty

I like to work with data and write about it. Find me on https://www.linkedin.com/in/kasham/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store