0) Это часть моего df:
target withCR_RF withCR_ET withCR_LG withCR_Ridge
0 partA 0.102 0.100 -0.3 -0.3
1 partB 0.081 0.085 -0.3 -0.3
2 part -0.063 -0.050 -0.3 -0.3

1) Это код, который я использую для графика:
from matplotlib.colors import ListedColormap
cmap = ListedColormap(['#96C3EB', '#299438', '#B8B8B8', '#CCAC93'])
ax = df.plot.bar(x='target', colormap=cmap)
ax.set_xlabel(None)
ax.set_ylabel('prediction performance R²')
ax.set_title('comparing pipelines of prediction of executive function variables')
plt.xticks(rotation=45, ha='right')
plt.show()
2) Теперь я хотел бы реализовать штриховки так же, как и цвета. Я не хочу, чтобы все столбцы были заштрихованы, а только первый, поэтому я пробую это, но это не работает.
cmap = ListedColormap(['#96C3EB', '#299438', '#B8B8B8', '#CCAC93'])
hatches = ['++', '', '', '']
ax = df.plot.bar(x='target', colormap=cmap, hatch=hatches)
ax.set_xlabel(None)
ax.set_ylabel('prediction performance R²')
ax.set_title('comparing pipelines of prediction of executive function variables')
plt.xticks(rotation=45, ha='right')
plt.show()
Я либо заштрихую все столбцы, либо ни один из них не заштрихую. Мне не нужны штриховки для всех стержней и я не хочу адаптировать каждый стержень вручную.
3) @Tranbi, спасибо за ваше предложение. Теперь это работает, но не для всех голубых полос. Есть шесть голубых полосок с условием withCR_RF (из-за 6 целей), но из них изменился только один столбец. Как я могу изменить все шесть из них?
cmap = ListedColormap(['#96C3EB', '#299438', '#B8B8B8', '#CCAC93'])
#hatches = ['++', '', '', '']
#hatches = ['', '++', '', '', '', '', 'xx', '', '', '']
ax = df.plot.bar(x='target', colormap=cmap, hatch=hatches)
ax.set_xlabel(None)
ax.set_ylabel('prediction performance R²')
ax.set_title('comparing pipelines of prediction of executive function variables')
plt.xticks(rotation=45, ha='right')
for lbl, patch in zip(ax.get_xticklabels() , ax.patches):
if lbl.get_text() == 'withCR_RF':
patch.set_hatch('*')
patch.set_edgecolor(patch.get_facecolor())
patch.set_facecolor('none')
plt.show()

4) @Mozway, спасибо за ответ. Ваш последний вариант идеально подходит для моих нужд, но можно ли индексировать более одного бара? Это не работает, к сожалению. Я бы хотел, чтобы все голубые полосы были заштрихованы.
cmap = ListedColormap(['#96C3EB', '#299438', '#B8B8B8', '#CCAC93'])
hatches = ['++', '', '', '']
hatches = ['', '++', '', '', '', '', 'xx', '', '', '']
ax = df.plot.bar(x='target', colormap=cmap)
ax.patches[0,1,2,3].set_hatch('+') #see here please
ax.set_xlabel(None)
ax.set_ylabel('prediction performance R²')
ax.set_title('comparing pipelines of prediction of executive function variables')
plt.xticks(rotation=45, ha='right')
plt.show()
Подробнее: https://stackoverflow.com/questions/760 ... f-interest
Мобильная версия