matplotlib子图(barplot):子图中某些条的不同颜色

发布于 2025-02-09 12:13:06 字数 1326 浏览 0 评论 0原文

我有几个带有共享轴的子图。在每个子图中,我都希望“回忆”某种颜色的栏,“精度”的特定颜色等等。请参阅下面由以下代码产生的链接:

# 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!

Subplot with shared axis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷→糖丶微凉 2025-02-16 12:13:06

感谢您提供数据。我更新了代码,以使每个子图中的颜色都具有您需要的3种颜色。请看看是否可以。但是,传说被禁用。

import pandas as pd
import matplotlib.pyplot as plt
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)

ax = df.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(20, 18), layout=(4, 4), legend=False)

labels = df.index.get_level_values(1).unique()
for grow in ax: #For each row of graphs
    for grph in grow: #For each graph in the specific row
        for p, color in zip(grph.patches, ["red", "yellow", "black"]): #For each bar in graph
            p.set_facecolor(color)
#        grph.legend(labels) #Enabling this only gives the first row - Recall & Red box

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)

输出图

”在此处输入图像描述”

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.

import pandas as pd
import matplotlib.pyplot as plt
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)

ax = df.unstack(level=0).plot(kind='bar', subplots=True, rot=0, figsize=(20, 18), layout=(4, 4), legend=False)

labels = df.index.get_level_values(1).unique()
for grow in ax: #For each row of graphs
    for grph in grow: #For each graph in the specific row
        for p, color in zip(grph.patches, ["red", "yellow", "black"]): #For each bar in graph
            p.set_facecolor(color)
#        grph.legend(labels) #Enabling this only gives the first row - Recall & Red box

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)

Output graph

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文