Learning On Your Own: Plot with Less Code | Mathaus Silva
1 - Introduction: Easier and Faster
In some cases, you can plot data directly from pandas without needing to use matplotlib. Medium's author Andre Ye argues matplotlib, despite "generally considered to be the simplest way to create visualizations in Python", is a far inferior when compared to pandas. Instead, he believes pandas provides a simpler, quicker plots for analysis and a more convenient and direct interface for data manipulation and visualization.
2.1 - Creating and Plotting Visualization: Bar Charts, Stacked Bar Charts, and Horizontal Bar Charts
Firstly, pandas makes it easier for you to create plots directly. You're able to convert between bar charts, stacked bar charts, and horizontal bar charts just by changing the parameter value.
Consider this example directly from Andre Ye's blog post. By importing libraries pandas and numpy, the following DataFrame is randomly generated with four columns (A, B, C, and D) and ten rows.
"data.head()" returns the first five rows of each column. In order to plot the values of each of the columns per row, we simply use "data.plot.bar()".
The bar chart above displays the values of each of the columns per row. However, smaller values are made difficult to see in this graph (i.e. 4B). If you wish to stack the columns for a more clear visualization, you just need to add "stacked=True" inside the parameter value.
Now, having adjusted the width of each bar by itself, you can more clearly see 4B in comparison to other columns inside his row.
Or, try using "data.plot.barh", which changes the bar's orientation from vertical to horizontal.
By using pandas, all these variants can be easily created with only one line of code because they establish a direct flow with the data. Yet, the same cannot be said to matplotlib.
2.2 - Creating and Plotting Visualization: matplotlib
Now that we've seen pandas' practicality and simplicity when creating visualization, I would like to show what it would take for matplotlib to plot the same bar chart we plotted in pandas.
First, we would have to import matplotlib, assign variable x to return evenly spaced values of 10 rows, and declare our width. Then, plot every column from DataFrame 'data' in manually along with their color, width, and label. And finally, create a for loop for labels, rotate them vertically and apply the best location for the legend.
Compared to pandas' one-liner, matplotlib requires 13 lines of code to produce a similar yet simple bar chart. This is because pandas does a lot of inference in what we want it to plot, so it can visualize what we want it to in many cases without explicitly declaring them.
3 - Data Manipulation Functions: Differencing and Rolling Means
One of the major benefits of using pandas directly is being able to use their DataFrame manipulations. Our first example is using "data.diff()", which returns the difference between one row and the row before it.
Since each row returns the difference between one row and the row before it, it is normal for the first row to return NaN values.
Now, using "data.diff()", we can plot out the differenced data using a box and whisker plot.
Once again, pandas' use of parameters showcases the simplicity and practicality of changing colors while plotting.
Another example of a handy data manipulation function in pandas is ".rolling().mean()". By taking the average rolling mean, you can reduce the noisiness of the dataset and provide rolling window calculations. As a comparison, we can plot the original data with noise (in blue) and the rolling mean (in orange) on a line graph.
While "data.diff()" and "data.rolling().mean()" are one of pandas' most useful data manipulation functions, there are other more advanced types of plots that can also be created directly from the data, such as "kde" or "density" for density plots, "scatter" for scatter plots, and "hexbin" for hexagonal bin plots.
For more information, make sure to check pandas' user guide on more advanced chart visualization tools as well as KGP Talkie's "Complete Data Visualization in Pandas" crash course on Youtube: 1. https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html 2. https://www.youtube.com/watch?v=0IKz-KxDCuM&t=2322s (Starting on 37:57)
4 - Subplots: Pie Graph and Line Graph
While using pandas, it is very simple to create subplots from the data. By specifying "subplots=True", pandas automatically creates subplots based on the DataFrame's columns. Considering our newly created DataFrame has columns "X" and "Y" with rows "A" through "E", "data.plot.pie()" will generate two pie graphs with five sections each.
If you wish to add more parameters to the pie graph, pandas allows for more customization. Some parameters include custom labels to the slices (labels=), color specification for each slice (colors=), and the size of the labels (fontsize=). If we were instead using matplotlib, we'd have to create two manually-separate subplots
Finally, pandas' simplicity and practicality can also be seen when plotting line graphs. In this example, subplots are used to graph four different line graphs from columns "A" through "D" using ".plot()".
Similar to the previous pie chart, you are also able to change a line graph's parameters. For example, by adding a parameter "layout=(2, 2)", pandas formats its subplots to the specified layout formatting.