我有几个图表(条形图、饼图)。每个图表中都有相同的类别(布朗克斯、皇后区......)。 但在每个图表中,类别(例如布朗克斯)都以不同的颜色显示。有人知道如何用相同的颜色为每个图表中的类别着色吗?
*my_c = ['绿色','蓝色','红色','黄色','黑色']
fig, ax = plt.subplots(nrows = 2, ncols = 2)
df.loc[:,'Stadtteil'].value_counts().plot(kind = 'bar', ax = ax[0][0], colors = my_c)
ax[0][0].set(xlabel = 'Stadtteil', ylabel = 'Anzahl', title = 'Anzahl Bezirke pro Stadtteil')
ax[0][0].xaxis.set_tick_params(labelrotation = 45)
pd.crosstab(
index = df.loc[:,'Stadtteil'],
columns = 'Einwohner',
values = df.loc[:,'Einwohner'],
aggfunc='sum').plot(ax = ax[0][1], kind = 'pie', legend = False, y = 'Einwohner', colors = my_c)
ax[0][1].set(ylabel = '')
df.groupby('Stadtteil')['Einwohner'].sum().sort_values(ascending=False).plot(kind = 'bar', ax = ax[1][1], colors = my_c)
ax[1][1].set(xlabel = 'Stadtteil', ylabel = 'Anzahl Einwohner', title = 'Anzahl Einwohner pro Stadtteil')
ax[1][1].xaxis.set_tick_params(labelrotation = 45)
fig.tight_layout()
plt.show()*
我尝试使用字典将颜色映射到类别,但这不起作用。颜色图的使用也没有成功。
您需要做的是将颜色与每个类别相关联。您可以像这样手动执行此操作
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Stadtteil': ['Bronx', 'Queens', 'Brooklyn', 'Manhattan', 'Staten Island', 'Bronx', 'Queens', 'Brooklyn', 'Manhattan', 'Staten Island'],
'Einwohner': [100, 200, 150, 250, 50, 120, 220, 180, 270, 60]
}
df = pd.DataFrame(data)
category_colors = {
'Bronx': 'green',
'Queens': 'blue',
'Brooklyn': 'red',
'Manhattan': 'yellow',
'Staten Island': 'black'
}
df['color'] = df['Stadtteil'].map(category_colors)
或者如果您有大量类别,则为动态:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Stadtteil': ['Bronx', 'Queens', 'Brooklyn', 'Manhattan', 'Staten Island', 'Bronx', 'Queens', 'Brooklyn', 'Manhattan', 'Staten Island'],
'Einwohner': [100, 200, 150, 250, 50, 120, 220, 180, 270, 60]
}
df = pd.DataFrame(data)
unique_stadtteils = df['Stadtteil'].unique()
n_categories = len(unique_stadtteils)
colormap = plt.cm.get_cmap('tab20', n_categories)
category_colors = {stadtteil: colormap(i) for i, stadtteil in enumerate(unique_stadtteils)}
然后,按照你所做的去做(将参考文献添加到字典中)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
stadtteil_counts = df['Stadtteil'].value_counts()
colors = [category_colors[stadtteil] for stadtteil in stadtteil_counts.index]
stadtteil_counts.plot(kind='bar', ax=ax[0][0], color=colors)
ax[0][0].set(xlabel='Stadtteil', ylabel='Anzahl', title='Anzahl Bezirke pro Stadtteil')
ax[0][0].xaxis.set_tick_params(labelrotation=45)
stadtteil_population = df.groupby('Stadtteil')['Einwohner'].sum()
colors = [category_colors[stadtteil] for stadtteil in stadtteil_population.index]
stadtteil_population.plot(ax=ax[0][1], kind='pie', legend=False, colors=colors)
ax[0][1].set(ylabel='')
colors = [category_colors[stadtteil] for stadtteil in stadtteil_population.sort_values(ascending=False).index]
stadtteil_population.sort_values(ascending=False).plot(kind='bar', ax=ax[1][1], color=colors)
ax[1][1].set(xlabel='Stadtteil', ylabel='Anzahl Einwohner', title='Anzahl Einwohner pro Stadtteil')
ax[1][1].xaxis.set_tick_params(labelrotation=45)
fig.tight_layout()
plt.show()
产生
以同样的方式添加其他图。