The methods to plot a data frame in Pandas (here & here) create us useful graphics. But as with Matplotlib, sometimes they do not fully meet our requirements. Let us find out how we can fix that in Pandas.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Name your axes
We can name our X- and Y-axes and set a title for our plot with this code:
1 2 3 |
df.plot(ylabel="Temperature in °C", xlabel="Months", title="Average Day Temperatures") |
The basic plot from Pandas now has a title and labels for the X- and the Y axes:
Hide the legend
If you only have one line to plot, you can hide the legend:
1 |
df["Bern"].plot(legend=False) |
The generated plot has no legend:
Show the grid
We can show a grid in our plot:
1 |
df.plot(grid=True) |
Depending on the values, the grid can help us to read the data more easily:
Change the line style
For line plots we can change the style of the line and the colour:
1 |
df["Bern"].plot(style="g--") |
This uses a dashed green line for the plot:
Change the plot size
We can use the figsize parameter to change the size of our plot:
1 |
df.plot(figsize=(3, 3)) |
This changes the size of our plot to 3 x 3 inches:
Style your plots
We can use the Matplotlib styles for our Pandas plots:
1 2 3 4 |
import matplotlib.pyplot as plt with plt.style.context('fivethirtyeight'): df.plot(figsize=(8,3)) |
This renders our plot with the style “fivethirtyeight“:
Use the underlying Matplotlib objects directly
If you know how to do the customisation in Matplotlib but not in Pandas, you can always leave the abstraction behind and work directly on the Matplotlib objects:
1 2 3 4 5 6 7 8 9 10 |
from matplotlib.ticker import MultipleLocator ax = df.plot(grid=True) ax.set_ylabel("Temperature in °C") ax.set_title("Average Day Temperatures") ax.xaxis.set_major_locator(MultipleLocator(1)) ax.tick_params(axis='x',which='major',rotation=90) ax.set_xticks(np.arange(len(months))) ax.set_xticklabels(months) |
This allows us to customise all aspects of the plots as we could do it directly in Matplotlib:
Change the colours in the box plot
The box plot allows us to style the different parts of the plot:
1 2 3 4 5 6 7 8 |
color = { "boxes": "DarkGreen", "whiskers": "DarkOrange", "medians": "DarkBlue", "caps": "Gray", } df.plot.box(color=color) |
This adds a lot more colour to the box plot:
Show the values in a pie chart
If we want to see the percentages in the wedge of a pie chart, we can use this code:
1 |
df.plot.pie(y='Animals',autopct="%.2f") |
This shows us the percentage value for each part:
If we want to show the effective values from the data, we need to calculate that value from the percentage and the sum of all values:
1 2 |
df.plot.pie(y='Animals', autopct=lambda x: '{:.0f}'.format(x*df["Animals"].sum()/100)) |
This gives us the same values as in the data frame:
Next
We now can plot our data frames and change their appearance to our needs. But what happens if we have messy data? Next week we clean-up a data source to use it in Pandas.
1 thought on “Python Friday #177: Customise Your Plots in Pandas”