根据数据框列值定义 matplotlib barh 的条形颜色

Define bar colors of matplotlib's barh based on dataframe column values

我想使用 matplotlib 的 barh 函数将 table 绘制成直方图。 table 是一个 pandas 数据框,其结构为:

test是自变量,count是因变量。我希望不同 species(蓝色、绿色、橙色等)的条形图颜色不同,所以我尝试了代码

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(data={'test': np.linspace(1,16,16).astype(int),
                        'count': np.random.randint(30,120,16),
                        'species': ['species 1','species 1','species 1','species 2','species 1','species 1','species 2','species 2','species 1','species 2','species 2','species 2','species 1','species 1','species 1','species 1']
                       }
                 )

plt.figure(figsize=(14,6))
plt.barh(data=df, y='test', width='count')
# plt.barh(data=df, y='test', width='count', color='species', color_map={'species 1':'b', 'species 2':'g'})
plt.xlabel('frequency')
plt.ylabel('test number')

plt.show()

但我只能将条形设为一种颜色:

您需要将物种作为数据框的索引,然后使用.loc 单独绘制物种图。我还添加了一个图例,但可以随意删除它:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame(data={'test': np.linspace(1,16,16).astype(int),
                        'count': np.random.randint(30,120,16),
                        'species': ['species 1','species 1','species 1','species 2','species 1','species 1','species 2','species 2','species 1','species 2','species 2','species 2','species 1','species 1','species 1','species 1']
                       }
                 )

# Set the index to species
df.set_index('species', inplace=True)

# Seperately graph the species
plt.barh(data=df.loc['species 1'], y='test', width='count', label='species 1')
plt.barh(data=df.loc['species 2'], y='test', width='count', label='species 2')

# Add a legend to the graph
plt.legend()

# plt.barh(data=df, y='test', width='count', color='species', color_map={'species 1':'b', 'species 2':'g'})
plt.xlabel('frequency')
plt.ylabel('test number')

plt.show()

这导致了下图: