我有三个大熊猫的情节。在其中一个图中没有数字" 2"所以类别的所有颜色都与另外两个颜色不同。是否可以使用固定的色彩图?
以下是三个图:
以下是其中一个地块的构建方式:
fig, ax = plt.subplots()
columns = ['Radius', 'FRC', 'Scoring']
df = pd.DataFrame(bestof10, columns=columns)
out = {}
for column in columns:
out[column] = pd.value_counts(df[column])
uniq_df = pd.DataFrame(out).fillna(0)
test = uniq_df.T.plot(kind="bar", stacked=True, ax =ax,rot=0)
#ax.legend(loc='best');
ax.set_ylabel("frequency")
plt.legend(bbox_to_anchor=(1.2, 1), loc='upper right', ncol=1)
plt.savefig("WithoutInfluenceOfParameterBestOf10.png",bbox_inches='tight')
答案 0 :(得分:2)
实现这一目标的一种方法是填写数据框中的所有不存在的值(我在设置图例时遇到问题,这就是我使用GridSpec的原因):
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import gridspec
gs = gridspec.GridSpec(ncols = 5, nrows = 12)
fig = plt.figure(figsize=(4,8))
axes = [
fig.add_subplot(gs[4*i:4*(i+1),:-1]) for i in range(3)
]
columns = ['Radius', 'FRC', 'Scoring']
dfs = [
pd.DataFrame({
'FRC' : sum([[i+1]*n for i,n in enumerate([0,0,3,7,0,0,0,])],[]),
'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,1,9,0,0,0,])],[]),
'Scoring' : sum([[i+1]*n for i,n in enumerate([1,0,1,2,1,2,3,])],[]),
}),
pd.DataFrame({
'FRC' : sum([[i+1]*n for i,n in enumerate([0,1,8,11,0,0,0,])],[]),
'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,5,15,0,0,0,])],[]),
'Scoring' : sum([[i+1]*n for i,n in enumerate([3,2,3,3, 2,3,4,])],[]),
}),
pd.DataFrame({
'FRC' : sum([[i+1]*n for i,n in enumerate([0,1,8,11,0,0,0,])],[]),
'Radius' : sum([[i+1]*n for i,n in enumerate([0,0,5,15,0,0,0,])],[]),
'Scoring' : sum([[i+1]*n for i,n in enumerate([3,2,3,3, 2,3,4,])],[]),
}),
]
#collect all possible values in a set() by looping through all
#dataframes
all_vals=set()
for df in dfs:
for column in columns:
all_vals.update(df[column])
#looping through the data frames again to produce the plots
for bestof,ax in zip(dfs, axes):
df = pd.DataFrame(bestof, columns=columns)
out = {}
for column in columns:
out[column] = pd.value_counts(df[column])
for val in all_vals:
if val not in out[column]:
out[column][val] = 0
uniq_df = pd.DataFrame(out).fillna(0)
test = uniq_df.T.plot(kind="bar", stacked=True, ax=ax, rot=0)
ax.legend(loc='best');
ax.set_ylabel("frequency")
ax.legend(bbox_to_anchor=(1.2, 1), loc='upper right', ncol=1)
fig.tight_layout()
plt.show()
希望这有帮助。