Plotting with Pandas works without much effort. Today we look at the two dedicated methods hist() and boxplot() directly on the data frame.
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.
Box plot
For the box plots we reuse the data frame with the temperatures from the last post. If we use df.plot.box() on the whole data frame, we get a box for each city:
1 |
df.plot.box() |
We can also use the method df.boxplot() directly on the data frame to create the box plot:
1 |
df.boxplot() |
It gives us the same representation, but it shows the grid by default:
We can style the output of the df.boxplot() method, but I prefer the df.plot.box() approach.
Histograms
For the histogram we need a new data frame with test scores for 3 candidates:
1 2 3 4 5 6 7 |
scores = { "a": [1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 6, 6], "b": [2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5], "c": [1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4]} df = pd.DataFrame(data=scores) df["a"].plot.hist() |
If we filter for the column “a”, the histogram shows us an expected plot:
To get a histogram for each candidate, we may try this call without a filter:
1 |
df.plot.hist() |
Unfortunately, that creates one single histogram over the whole data frame:
To get the expected result, we need to df.hist() method directly in the data frame:
1 |
df.hist(layout=(1,3)) |
The call to the Matplotlib specific feature for the layout is only needed to get the 3 histograms next to each other:
Next
The two methods hist() and boxplot() directly on the data frame are a little bit different compared to the other plot() methods. Nonetheless, we can use those methods to create the plots we want. Next week we explore the options to style and customise our plots in Pandas.
3 thoughts on “Python Friday #176: Visualise Data in Pandas With Hist() & Boxplot()”