如何用fig、ax制作多个图表

问题描述 投票:0回答:1

我想知道如何制作多个条形图。

我用 groupby 制作了一个数据框,如下所示

我想要的是在分开的两个条形图上显示实际价格和折扣价格的最低、最高价格。

起初,我只是尝试使用此代码制作图表

df.plot(kind='bar', y='actual_price', stacked=False)

但是这个只显示一张关于实际价格的最小值和最大值的图表。

我也尝试使用 fig, ax = plt.subplot(1,2, Figsize(10,10) 。 但总是报错

ax[0].bar(df.actual_price, stack=False)

如何解决这个问题? 如何知道高度是多少?那我必须更改数据框吗?

请分享您的意见。

谢谢

pandas matplotlib graph charts axis
1个回答
0
投票

假设使用 MultiIndex,您可能需要

stack
您的数据集并使用
seaborn.catplot

import seaborn as sns

sns.catplot(df.stack([0, 1])
              .rename_axis(['category', 'type', 'indicator'])
              .reset_index(name='value'),
            x='category', y='value', col='type', hue='indicator',
            kind='bar')

输出:

可重复输入:

df = pd.DataFrame.from_dict({'index': ['Car', 'House', 'IT'],
                             'columns': [('actual_price', 'min'),
                              ('actual_price', 'max'),
                              ('discount_price', 'min'),
                              ('discount_price', 'max')],
                             'data': [[10, 100, 50, 50], [5, 8, 3, 4], [1, 7, 1, 5]],
                             'index_names': [None],
                             'column_names': [None, None],
                             }, orient='tight')
© www.soinside.com 2019 - 2024. All rights reserved.