Making Simple Graphs with Matplotlib
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
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.barbecause it is a bar graph
plt.showis 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.
fontdict assists in changing the font size, weight and color. Feel free to experiment with the entire set of ‘RGB’ colors in
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
- Length — Length of different fins
NumPy arrays for the above facts and dimensions.
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.annotateto highlight the bubbles
- Understand the function of s
In Simple Words
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