如何制作改变颜色的分组水平线图和几个子图



在Python中,我想创建一个包含三个子图的水平条图,其中每个面板有五组,每组四个条,同时还更改了条的自动颜色。

基于下面的代码,这是受此指导的启发,我如何a)改变每组中的条的颜色(例如深蓝色,浅蓝色,深紫色,浅紫色),以及b)制作一个具有三个这些面板的绘图,而不仅仅是一个(例如,它可以与我通常用于几个子绘图的代码相结合)fig, (ax1, ax2, ax3) = plt.subplots(1, 3)?

`speed = [40, 48, 52, 69, 88]
lifespan = [70, 1.5, 25, 12, 28]
height = [35, 5, 18, 17, 43]
width = [40, 18, 35, 37, 15]
index = ['elephant', 'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed, 'lifespan': lifespan, 'height': height, 'width': width}, index=index)
ax = df.plot.barh()`

您可以使用{column_name:color_code}的字典在df.plot.barh()中定义颜色,

ax = df.plot.barh(color={"speed": "#08519c", "lifespan": "#6baed6","height":'#54278f',"width":'#bcbddc'})

或颜色列表,

ax = df.plot.barh(color=["#08519c", "#6baed6",'#54278f','#bcbddc'])

对于多个面板,将绘图分配到不同的轴上,例如

fig, axes = plt.subplots(1, 3)
for i in range(3):
df_subset = ...
axes[i] = df_subset.plot.barh(color={"speed": "#08519c", "lifespan": "#6baed6","height":'#54278f',"width":'#bcbddc'})

最新更新