Pedro Henrique
Pedro Henrique

Reputation: 300

How can I plot multiples columns with completely diffent meaning in same plot

I have the follow data. How can I plot the three columns together (dist, price and count) for each neighbourhood?

t1 = pd.DataFrame({'neighbourhood': ['Allston-Brighton', 'Back Bay', 'Beacon Hill', 'Brookline', 'Cambridge'], 
                   'dist': [5.318724014750601, 0.3170049667872781, 1.2481192434918875, 4.122402023894361, 2.975557190604119],
                   'price':[130.39048767089844, 276.3820495605469, 231.87042236328125, 127.90569305419922, 195.94696044921875],
                  'count':[238, 239, 135, 7, 7]})


    neighbourhood       dist        price       count
0   Allston-Brighton    5.318724    130.390488  238
1   Back Bay            0.317005    276.382050  239
2   Beacon Hill         1.248119    231.870422  135
3   Brookline           4.122402    127.905693  7
4   Cambridge           2.975557    195.946960  7

Do you any suggestion with matplotlib, or seaborn ? Thank you!

Upvotes: 1

Views: 76

Answers (1)

Derek O
Derek O

Reputation: 19565

What you want is called a grouped barplot. You can melt the dataframe, which reformats your dataframe in a long format i.e. each neighbourhood and one variable at a time per row. Then apply the seaborns barplot to this melted dataframe.

Update: since the y-values are far apart, we can add some labels. This is a workaround that goes through each row, and appends the label to the corresponding bar. The values you add to x and y are just by trial and error.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

t1 = pd.DataFrame({'neighbourhood': ['Allston-Brighton', 'Back Bay', 'Beacon Hill', 'Brookline', 'Cambridge'], 
                   'dist': [5.318724014750601, 0.3170049667872781, 1.2481192434918875, 4.122402023894361, 2.975557190604119],
                   'price':[130.39048767089844, 276.3820495605469, 231.87042236328125, 127.90569305419922, 195.94696044921875],
                  'count':[238, 239, 135, 7, 7]})

t1_melted = pd.melt(t1, id_vars="neighbourhood", var_name="source", value_name="value_numbers")
g = sns.barplot(x="neighbourhood", y="value_numbers", hue="source", data=t1_melted)

for index, row in t1.iterrows():
     g.text(row.name - 0.35, row.dist + 0.1, round(row.dist, 2), color='black')

plt.show()

enter image description here

Upvotes: 1

Related Questions