matplotlib子图(barplot):子图中某些条的不同颜色
我有几个带有共享轴的子图。在每个子图中,我都希望“回忆”某种颜色的栏,“精度”的特定颜色等等。请参阅下面由以下代码产生的链接:
# data
values = list([['nan', 'nan', 0.25, 0.25],
['nan', 'nan', 0.813, 0.413],
['nan', 'nan', 0.13793, 0.793],
[0.5, 0.4666, 0.6666666, 0.75],
[0.725, 0.13, 0.444, 0.5],
[0.33337, 0.7776, 0.53,
0.86],
[0.296, 0.33, 0.68,
0.4722],
[0.158, 0.166, 0.45,
0.3477],
[0.22063, 0.2218, 0.54,
0.7778],
[0.8421, 0.199, 0.57,
0.4333],
[0.36842105, 0.545, 0.386,
0.155567],
[0.62281, 0.23394, 0.46,
0.36667]])
index_1 = ['1', '2', '4', '5']
index_2 = ['Recall', 'Precision', 'F1']
iterables = [index_1, index_2]
index = pd.MultiIndex.from_product(iterables, names=["ClusterID", "Metric"])
df = pd.DataFrame(values, columns = ['Method1', 'Method2', 'Method3', 'Method4'], index = index)
c = ['red', 'yellow', 'black']
ax = df.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(20, 18), layout=(4, 4), color = c)
def_ylim = (0.0, 0.8)
plt.setp(ax, ylim = def_ylim)
plt.tight_layout(pad=0.6, w_pad=0.6, h_pad=1.0)
我该如何实现?我的数据框是熊猫中的多索引数据框。我正在尝试为演示准备结果,而我对绘图是相当陌生的。
感谢您的投入!
I have several subplots with a shared axis. In each subplot I would like the bar of 'Recall' a certain color, the bar of 'Precision' a certain color and so on. Please see the link below that resulted from following code:
# data
values = list([['nan', 'nan', 0.25, 0.25],
['nan', 'nan', 0.813, 0.413],
['nan', 'nan', 0.13793, 0.793],
[0.5, 0.4666, 0.6666666, 0.75],
[0.725, 0.13, 0.444, 0.5],
[0.33337, 0.7776, 0.53,
0.86],
[0.296, 0.33, 0.68,
0.4722],
[0.158, 0.166, 0.45,
0.3477],
[0.22063, 0.2218, 0.54,
0.7778],
[0.8421, 0.199, 0.57,
0.4333],
[0.36842105, 0.545, 0.386,
0.155567],
[0.62281, 0.23394, 0.46,
0.36667]])
index_1 = ['1', '2', '4', '5']
index_2 = ['Recall', 'Precision', 'F1']
iterables = [index_1, index_2]
index = pd.MultiIndex.from_product(iterables, names=["ClusterID", "Metric"])
df = pd.DataFrame(values, columns = ['Method1', 'Method2', 'Method3', 'Method4'], index = index)
c = ['red', 'yellow', 'black']
ax = df.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(20, 18), layout=(4, 4), color = c)
def_ylim = (0.0, 0.8)
plt.setp(ax, ylim = def_ylim)
plt.tight_layout(pad=0.6, w_pad=0.6, h_pad=1.0)
How could I achieve this? My dataframe is MultiIndex DataFrame in pandas. I'm trying to prep my results for a presentation and I'm fairly new to plotting.
Thanks for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您提供数据。我更新了代码,以使每个子图中的颜色都具有您需要的3种颜色。请看看是否可以。但是,传说被禁用。
输出图
Thanks for providing the data. I updated the code so that the colors within each subplot has the 3 colors as you needed. Please see if this is ok. However, the legend is disabled.
Output graph