Может ли кто-нибудь помочь мне запрограммировать следующий набор данных, используя pandas и matplotlib, чтобы создать график, который выглядит как добавленный ive или что-то отдаленно похожее. Любая помощь будет означать мир
df1=df.loc[[0,1,2,3,4,5,6,7]]
print(df1)
partNames=['Conservative', 'Labour', 'Lib Dem', 'SNP', 'Green Party',
'Brexit', 'Plaid','Other']
results = {
'18-19':[df1.loc[0, 'Groups' : 'Other']],
'20-24':[df1.loc[1, 'Groups' : 'Other']],
'25-29':[df1.loc[2, 'Groups' : 'Other']],
'30-39':[df1.loc[3, 'Groups' : 'Other']],
'40-49':[df1.loc[4, 'Groups' : 'Other']],
'50-59':[df1.loc[5, 'Groups' : 'Other']],
'60-69':[df1.loc[6, 'Groups' : 'Other']],
'70+':[df1.loc[7, 'Groups' : 'Other']],
}
def survey(results, partNames ):
labels = list(results.keys())
data = np.arange(len(df1.Groups))
data_cum = data.cumsum(axis=0)
category_colors = plt.get_cmap('RdYlGn')(
np.linspace(0.15, 0.85, data.shape[]))
fig, ax = plt.subplots(figsize=(9.2, 5))
ax.invert_yaxis()
ax.xaxis.set_visible(False)
ax.set_xlim(0, np.sum(data, axis=1).max())
for i, (colname, color) in enumerate(zip(partNames, category_colors)):
widths = data[:, i]
starts = data_cum[:, i] - widths
ax.barh(labels, widths, left=starts, height=0.5,
label=colname, color=color)
xcenters = starts + widths / 2
r, g, b, _ = color
text_color = 'white' if r * g * b < 0.5 else 'darkgrey'
for y, (x, c) in enumerate(zip(xcenters, widths)):
ax.text(x, y, str(int(c)), ha='center', va='center',
color=text_color)
ax.legend(ncol=len(partNames), bbox_to_anchor=(0, 1),
loc='lower left', fontsize='small')
return fig, ax
survey(results, partNames)
plt.show()
Я продолжаю получать ошибки, такие как кортеж индекс вне диапазона или ошибки трассировки, в частности в: np.linspace (0.15, 0.85, data.shape []))