如何根据单独的 pandas 列为条形图着色
How to color bars based on a separate pandas column
我需要绘制一个条形图并根据我的数据框的“属性”列应用颜色
x 轴 = 份额
y 轴 = 价格
fig, ax = plt.subplots()
ax.barh(df['Share'],df['Price'], align='center')
ax.set_xlabel('Shares')
ax.set_ylabel('Price')
ax.set_title('Bar Chart & Colors')
plt.show()
感谢您的帮助!
- 有两种简单的方法可以为
'Attribute'
绘制不同颜色的条形图
- 使用
.pivot
and then plot with pandas.DataFrame.plot
转换数据框并为水平条形图指定 kind='barh'
- 如果使用
kind='bar'
,索引将是 x 轴,如果使用 kind='barh'
,索引将是 y 轴
- 转换后的数据框的每列都将用单独的颜色绘制。
pandas
使用 matplotlib
作为默认绘图后端。
- 将
seaborn.barplot
与 hue='Attribute'
和 orient='h'
结合使用。如 OP 中所示,此选项适用于长格式的数据框。
seaborn
是 matplotlib
的高级 API
- 使用
pandas 1.3.0
、seaborn 0.11.1
和 matplotlib 3.4.2
进行了测试
导入和 DataFrame
import pandas as pd
import seaborn as sns
# test dataframe
data = {'Price': [110, 105, 119, 102, 111, 117, 110, 110], 'Share': [110, -50, 22, 79, 29, -2, 130, 140], 'Attribute': ['A', 'B', 'C', 'D', 'A', 'B', 'B', 'C']}
df = pd.DataFrame(data)
Price Share Attribute
0 110 110 A
1 105 -50 B
2 119 22 C
3 102 79 D
4 111 29 A
5 117 -2 B
6 110 130 B
7 110 140 C
pandas.DataFrame.plot
# transform the dataframe with .pivot
dfp = df.pivot(index='Price', columns='Attribute', values='Share')
Attribute A B C D
Price
102 NaN NaN NaN 79.0
105 NaN -50.0 NaN NaN
110 110.0 130.0 140.0 NaN
111 29.0 NaN NaN NaN
117 NaN -2.0 NaN NaN
119 NaN NaN 22.0 NaN
# plot
ax = dfp.plot(kind='barh', title='Bar Chart of Colors', figsize=(6, 4))
ax.set(xlabel='Shares')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')
- 和
stacked=True
ax = dfp.plot(kind='barh', stacked=True, title='Bar Chart of Colors', figsize=(6, 4))
seaborn.barplot
- 请注意,与之前的绘图相比,y 轴值的顺序是相反的
ax = sns.barplot(data=df, x='Share', y='Price', hue='Attribute', orient='h')
ax.set(xlabel='Shares', title='Bar Chart of Colors')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')
我需要绘制一个条形图并根据我的数据框的“属性”列应用颜色
x 轴 = 份额
y 轴 = 价格
fig, ax = plt.subplots()
ax.barh(df['Share'],df['Price'], align='center')
ax.set_xlabel('Shares')
ax.set_ylabel('Price')
ax.set_title('Bar Chart & Colors')
plt.show()
感谢您的帮助!
- 有两种简单的方法可以为
'Attribute'
绘制不同颜色的条形图- 使用
.pivot
and then plot withpandas.DataFrame.plot
转换数据框并为水平条形图指定kind='barh'
- 如果使用
kind='bar'
,索引将是 x 轴,如果使用kind='barh'
,索引将是 y 轴
- 转换后的数据框的每列都将用单独的颜色绘制。
pandas
使用matplotlib
作为默认绘图后端。
- 如果使用
- 将
seaborn.barplot
与hue='Attribute'
和orient='h'
结合使用。如 OP 中所示,此选项适用于长格式的数据框。seaborn
是matplotlib
的高级 API
- 使用
- 使用
pandas 1.3.0
、seaborn 0.11.1
和matplotlib 3.4.2
进行了测试
导入和 DataFrame
import pandas as pd
import seaborn as sns
# test dataframe
data = {'Price': [110, 105, 119, 102, 111, 117, 110, 110], 'Share': [110, -50, 22, 79, 29, -2, 130, 140], 'Attribute': ['A', 'B', 'C', 'D', 'A', 'B', 'B', 'C']}
df = pd.DataFrame(data)
Price Share Attribute
0 110 110 A
1 105 -50 B
2 119 22 C
3 102 79 D
4 111 29 A
5 117 -2 B
6 110 130 B
7 110 140 C
pandas.DataFrame.plot
# transform the dataframe with .pivot
dfp = df.pivot(index='Price', columns='Attribute', values='Share')
Attribute A B C D
Price
102 NaN NaN NaN 79.0
105 NaN -50.0 NaN NaN
110 110.0 130.0 140.0 NaN
111 29.0 NaN NaN NaN
117 NaN -2.0 NaN NaN
119 NaN NaN 22.0 NaN
# plot
ax = dfp.plot(kind='barh', title='Bar Chart of Colors', figsize=(6, 4))
ax.set(xlabel='Shares')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')
- 和
stacked=True
ax = dfp.plot(kind='barh', stacked=True, title='Bar Chart of Colors', figsize=(6, 4))
seaborn.barplot
- 请注意,与之前的绘图相比,y 轴值的顺序是相反的
ax = sns.barplot(data=df, x='Share', y='Price', hue='Attribute', orient='h')
ax.set(xlabel='Shares', title='Bar Chart of Colors')
ax.legend(title='Attribute', bbox_to_anchor=(1, 1), loc='upper left')
ax.grid(axis='x')