import matplotlib.pyplot as plt
import pandas as pd
import random
d = {a: [random.randint(2, 5), random.randint(3, 7)] for a in list('abcdefghij')}
df = pd.DataFrame(d)
my_colors0 = [plt.cm.plasma(i / len(df.columns) / 2 + 0.5) for i in range(len(df.columns))]
ax = df.T[0].plot(kind='bar', color=my_colors0, width=0.7)
my_colors1 = [plt.cm.plasma(i / len(df.columns) / 2) for i in range(len(df.columns))]
ax = df.T[1].plot(kind='bar', bottom=df.T[0], color=my_colors1, width=0.7, ax=ax)
plt.show()
如果从多个列(或行)绘制条形图并使用transpose.T,pandas将为每个列指定不同的颜色。所以在你的箱子里只有两种不同的颜色。将使用颜色列表的前两个元素
如果每个条需要单独的颜色,则需要打印两次。第二次使用另一个作为底部
一些示例代码显示了它的工作原理:
相关问题 更多 >
编程相关推荐