Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Boxplot with Matplotlib

I am using python 3 and jupyter notebook. I have a pandas dataframe that is structured like this:

          location  price
Apr 25   ASHEVILLE   15.0
Apr 25   ASHEVILLE   45.0
Apr 25   ASHEVILLE   50.0
Apr 25   ASHEVILLE  120.0
Apr 25   ASHEVILLE  300.0
<class 'pandas.core.frame.DataFrame'>

I am simply trying to create a boxplot for each location to show the range of prices among items in each location.

When I ran the following code:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


plt.boxplot(postings)
plt.show()

It returned TypeError: unhashable type: 'slice'

like image 590
Keenan Burke-Pitts Avatar asked May 22 '17 18:05

Keenan Burke-Pitts


People also ask

How do you make a vertical box plot in Python?

Practical Data Science using Python Box Plot in Seaborn is used to draw a box plot to show distributions with respect to categories. To create a vertical Box Plot, use the seaborn. boxplot().

Which argument of boxplot () is used to create a filled boxplot?

boxplot . Some of the frequently used ones are, main -to give the title, xlab and ylab -to provide labels for the axes, col to define color etc. Additionally, with the argument horizontal = TRUE we can plot it horizontally and with notch = TRUE we can add a notch to the box.


2 Answers

I guess you require boxplot for each location in same graph. I modified given dataframe to add sample data for another location which looks like-

   date   location month  price
0    25  ASHEVILLE   Apr   15.0
1    25  ASHEVILLE   Apr   45.0
2    25  ASHEVILLE   Apr   50.0
3    25  ASHEVILLE   Apr  120.0
4    25  ASHEVILLE   Apr  300.0
5    25  NASHVILLE   Apr   34.0
6    25  NASHVILLE   Apr   55.0
7    25  NASHVILLE   Apr   70.0
8    25  NASHVILLE   Apr  105.0
9    25  NASHVILLE   Apr   85.0

Now, just call boxplot on this frame and provide parameters- column and by

postings.boxplot(column='price', by='location')

enter image description here

like image 136
Amey Dahale Avatar answered Sep 18 '22 20:09

Amey Dahale


I guess "price" is the column of data that you want to have boxplotted. So you need to first select this column and supply only that column to plt.boxplot.

u = u"""index,location,price
    Apr 25,ASHEVILLE,15.0
    Apr 25,ASHEVILLE,45.0
    Apr 25,ASHEVILLE,50.0
    Apr 25,ASHEVILLE,120.0
    Apr 25,ASHEVILLE,300.0"""

import io
import pandas as pd
import matplotlib.pyplot as plt

data = io.StringIO(u)

df = pd.read_csv(data, sep=",", index_col=0)

plt.boxplot(df["price"])
plt.show()

enter image description here

like image 21
ImportanceOfBeingErnest Avatar answered Sep 18 '22 20:09

ImportanceOfBeingErnest