答案 0 :(得分:2)
诀窍是创建一个多列数据框并使用stacked=True
选项。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({"Height" : [5,3,6,4],
"Color" : ["Blue", "Green", "Red", "Yellow"]})
color = []
for key,value in df.iterrows():
if(value['Color']=='Blue'):
color.append('b')
elif(value['Color']=='Green'):
color.append('g')
elif(value['Color']=='Red'):
color.append('r')
elif(value['Color']=='Yellow'):
color.append('y')
elif(value['Color']=='Orange'):
color.append('o')
else:
color.append('k')
df2 = pd.DataFrame(np.diag(df["Height"]), columns=df["Color"], index=df["Color"])
ax = df2.plot(kind='bar', title="Correlation", color=color, legend=True,
fontsize=12, stacked=True)
ax.set_xlabel("Colors", fontsize=12)
ax.set_ylabel("Height", fontsize=12)
plt.show()
答案 1 :(得分:0)