简体   繁体   English

平行图中相同的Y轴

[英]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)

在此处输入图片说明

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. 而且我不知道如何使每个图的Y轴上的值彼此成比例,并且与其他图的结果相关。 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? 另外,在我使用g1,g2,g3的代码中是否可以放入某些循环? Because in this form the code seems not elegant :) 因为这种形式的代码看起来并不优雅:)

I will be grateful for all the suggestions. 对于所有建议,我将不胜感激。

You can pass sharey=True to plt.subplots 您可以将sharey=True传递给plt.subplots

see the following docs for more info: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.subplots.html 有关更多信息,请参见以下文档: 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: 关于将子图放入循环中的第二个问题,您可以使用enumerate索引增加的列列表:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM