Tomasz Przemski
Tomasz Przemski

Reputation: 1127

Same Y-axis in parallel plot

I have this code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns;sns.set_style("whitegrid")
import seaborn as sns;
sns.set(style="ticks")

x = pd.read_csv("C:\\Users\dell\\Desktop\\delikatesy_owoce_i_warzywa(wyczyszczone).csv", sep=',')

x2 = x[x.grupa.str.contains('RAZEM') == False]

del x2['rok']
del x2['miesiac']
del x2['dzien_tygodnia_liczba']
del x2['dzien']
del x2['data2']
del x2['Unnamed: 0']

x2['data'] = pd.to_datetime(x2['data'])  
x3 = x2[x2["data"].isin(pd.date_range('2017-10-01','2017-10-18'))]

x4 = x3.groupby('grupa')['marza_netto'].agg({"marza_netto2": 'size'}).join(x3.groupby('grupa').sum()).reset_index()

x4.insert(4,'marza_procent2', x4['marza_procent']/x4['marza_netto2'])
del x4['marza_netto2']
del x4['marza_procent']

x4 = x4.set_index('grupa').T.rename_axis('DANE').reset_index().rename_axis(None,1)

flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]

fig, ax = plt.subplots(1,3)
g1 = sns.barplot(x="DANE", y="OWOCE", 
    palette=sns.color_palette(flatui),data=x4,ci=None, ax=ax[0])
g2 = sns.barplot(x="DANE", y="WARZYWA", 
    palette=sns.color_palette(flatui),data=x4,ci=None, ax=ax[1])
g3 = sns.barplot(x="DANE", y="NASIONA", 
    palette=sns.color_palette(flatui),data=x4,ci=None, ax=ax[2])

sns.despine()

g1.figure.set_size_inches(8,6)

g2.axes.set_title('Wpływ procentu marży na wielkosć sprzedaży',
    fontsize=15,color="b",alpha=0.3)

g1.set_xlabel("OWOCE",size = 15,color="g",alpha=0.5)
g2.set_xlabel("WARZYWA",size = 15,color="g",alpha=0.5)
g3.set_xlabel("NASIONA",size = 15,color="g",alpha=0.5)

g1.set_ylabel("",size = 20,color="r",alpha=0.5)
g2.set_ylabel("",size = 20,color="r",alpha=0.5)
g3.set_ylabel("",size = 20,color="r",alpha=0.5)

plt.setp(g1.get_xticklabels(), rotation=-45)
plt.setp(g2.get_xticklabels(), rotation=-45)
plt.setp(g3.get_xticklabels(), rotation=-45)

enter image description here

Where I compare three product groups separately. And I don't know how to make the values on the Y axis on each graph are proportional to each other and related to the results from other graphs. Because in this shape the results do not represent the actual proportions between the products.

In addition, does in places in the code where I use g1, g2, g3 can be put in some loop? Because in this form the code seems not elegant :)

I will be grateful for all the suggestions.

Upvotes: 2

Views: 652

Answers (1)

Johannes Wachs
Johannes Wachs

Reputation: 1310

You can pass sharey=True to plt.subplots

see the following docs for more info: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.subplots.html

For your second question on putting the subplots into a loop, you could use enumerate to loop over a list of your columns with an increasing index:

for index,name in enumerate(['OWOCE','WARZYWA','NASIONA']):
    gg = sns.barplot(x="DANE", y=name, palette=sns.color_palette(flatui),data=x4,ci=None, ax=ax[index])
    gg.set_xlabel(name,size = 15,color="g",alpha=0.5)
    gg.set_ylabel("",size = 20,color="r",alpha=0.5)
    plt.setp(gg.get_xticklabels(), rotation=-45)
    if index==1:
        gg.axes.set_title('Wpływ procentu marży na wielkosć sprzedaży',
        fontsize=15,color="b",alpha=0.3)

Upvotes: 3

Related Questions