python barh数据格式_来自pandas dataframe python的barh图表中行的不同颜色

似乎Pandas仅支持使用

colormap属性,该属性将相同的地图应用于图表中的每一行,例如:

df.plot(kind='barh', colormap='RdBu')

出于您的目的,您需要直接使用Matplotlib.

import matplotlib.pyplot as plt

import pandas as pd

df = pd.DataFrame({'a':[23, 13, 20, 17, 14, 22],

'b':[25, 21, 19, 14, 23, 11]},

index=['bob', 'john', 'paul', 'david', 'michael', 'neil'])

a_vals = df.a

b_vals = df.b

ind = np.arange(df.shape[0])

width = 0.35

# Set the colors

colors = ['b', 'g', 'r', 'c', 'm', 'y', 'g']

def autolabel(bars):

# attach some text labels

for bar in bars:

width = bar.get_width()

ax.text(width*0.95, bar.get_y() + bar.get_height()/2,

'%d' % int(width),

ha='right', va='center')

# make the plots

fig, ax = plt.subplots()

a = ax.barh(ind, a_vals, width, color = colors) # plot a vals

b = ax.barh(ind + width, b_vals, width, color = colors, alpha=0.5) # plot b vals

ax.set_yticks(ind + width) # position axis ticks

ax.set_yticklabels(df.index) # set them to the names

ax.legend((a[0], b[0]), ['a', 'b'], loc='center right')

autolabel(a)

autolabel(b)

plt.show()

请参考以下示例:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值