在Matplotlib中绘制多个直方图-颜色或并排条形图

SHASHA

问题:在Matplotlib中绘制多个直方图时,我无法将一个图与另一个图区分开

图像问题:** 问题**次要问题:左侧标签'Count'部分不在图像中。为什么?

描述

我想绘制3个不同集合的直方图。每个集合都是一个0和1的数组。我想要每个的直方图,以便我可以检测数据集上的不平衡。

我将它们分开绘制,但我希望将它们一起绘制图形。

可以并排放置不同的图形,也可以,或者我甚至用谷歌搜索将其绘制为3D,但我不知道“阅读”或“看”图形并理解它是多么容易。

现在,我想在同一图形的每一侧绘制[train],[validation]和[test]条,如下所示:

我想要这样

PS:我的Google搜索没有返回可以理解的任何代码。另外,我想知道是否有人检查即时消息对我的代码是否有任何疯狂

非常感谢你们!

代码 :

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):
    plt.figure()
    plt.clf()

    colors = ["b", "r", "m", "w", "k", "g", "c", "y"]

    information = []
    for index in xrange(0, len(Y)):
        y = Y[index]

        if index > len(colors):
            color = colors[0]
        else:
            color = colors[index]

        if labels is None:
            label = "?"
        else:
            if index < len(labels):
                label = labels[index]
            else:
                label = "?"

        unique, counts = np.unique(y, return_counts=True)
        unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

        for x in xrange(0, unique.shape[0]):
            unique_count[x, 0] = unique[x]
            unique_count[x, 1] = counts[x]

        information.append(unique_count)

        # the histogram of the data
        n, bins, patches = plt.hist(y, unique.shape[0], normed=False, facecolor=color, alpha=0.75, range=[np.min(unique), np.max(unique) + 1], label=label)

    xticks_pos = [0.5 * patch.get_width() + patch.get_xy()[0] for patch in patches]

    plt.xticks(xticks_pos, unique)

    plt.xlabel(xLabel)
    plt.ylabel(yLabel)
    plt.title(title)
    plt.grid(True)
    plt.legend()
    # plt.show()

    string_of_graphic_image = cStringIO.StringIO()

    plt.savefig(string_of_graphic_image, format='png')
    string_of_graphic_image.seek(0)

    return base64.b64encode(string_of_graphic_image.read()), information

编辑

遵循哈希码的答案,此新代码:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):
    plt.figure()
    plt.clf()

    colors = ["b", "r", "m", "w", "k", "g", "c", "y"]
    to_use_colors = []
    information = []


    for index in xrange(0, len(Y)):
        y = Y[index]

        if index > len(colors):
            to_use_colors.append(colors[0])
        else:
            to_use_colors.append(colors[index])

        unique, counts = np.unique(y, return_counts=True)
        unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

        for x in xrange(0, unique.shape[0]):
            unique_count[x, 0] = unique[x]
            unique_count[x, 1] = counts[x]

        information.append(unique_count)

    unique, counts = np.unique(Y[0], return_counts=True)
    histrange = [np.min(unique), np.max(unique) + 1]
    # the histogram of the data
    n, bins, patches = plt.hist(Y, 1000, normed=False, alpha=0.75, range=histrange, label=labels)


    #xticks_pos = [0.5 * patch.get_width() + patch.get_xy()[0] for patch in patches]

    #plt.xticks(xticks_pos, unique)

    plt.xlabel(xLabel)
    plt.ylabel(yLabel)
    plt.title(title)
    plt.grid(True)
    plt.legend()

正在产生这个:

结果

-新编辑:

def generate_histogram_from_array_of_labels(Y=[], labels=[], xLabel="Class/Label", yLabel="Count", title="Histogram of Trainset"):
    plt.figure()
    plt.clf()

    information = []

    for index in xrange(0, len(Y)):
        y = Y[index]

        unique, counts = np.unique(y, return_counts=True)
        unique_count = np.empty(shape=(unique.shape[0], 2), dtype=np.uint32)

        for x in xrange(0, unique.shape[0]):
            unique_count[x, 0] = unique[x]
            unique_count[x, 1] = counts[x]

        information.append(unique_count)

    n, bins, patches = plt.hist(Y, normed=False, alpha=0.75, label=labels)

    plt.xticks((0.25, 0.75), (0, 1))

    plt.xlabel(xLabel)
    plt.ylabel(yLabel)
    plt.title(title)
    plt.grid(True)
    plt.legend()

现在可以使用,但是左侧的标签有点超出范围,我想将条形图更好地居中...我该怎么做?

结果 : 在此处输入图片说明

哈希码55

我尝试过,并提出了这个建议。您可以在代码中更改xticks位置。您要做的只是将一个元组传递给plt.hist,难道不是更简单吧!?因此,假设您有两个0和1列表,那么您要做的是-

a = np.random.randint(2, size=1000)
b = np.random.randint(2, size=1000)
plt.hist((a, b), 2, label = ("data1", "data2"))
plt.legend()
plt.xticks((0.25, 0.75), (0, 1))

在此处输入图片说明

我尝试运行的确切代码(将垃圾箱数更改为2后)-

a = np.random.randint(2, size=1000)
b = np.random.randint(2, size=1000)
y = [a, b]
labels = ["data1", "data2"]
generate_histogram_from_array_of_labels(Y = y, labels = labels)

我得到了相同的结果...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

并排绘制条形图

来自分类Dev

Matplotlib并排条形图

来自分类Dev

R中并排的多个条形图

来自分类Dev

在Tableau中创建并排条形图

来自分类Dev

绘制快递条形图颜色变化

来自分类Dev

基于值的颜色matplotlib条形图

来自分类Dev

Python Matplotlib条形图颜色

来自分类Dev

在R中并排调整多个条形图的大小

来自分类Dev

更改条形图图例中的颜色

来自分类Dev

R ggplot中并排的堆积条形图

来自分类Dev

绘制多个条形图

来自分类Dev

matplotlib - 如何并排绘制条形图以比较 2 列之间的值

来自分类Dev

在R中的直方图上绘制条形图

来自分类Dev

绘制后更改条形图边缘的颜色

来自分类Dev

熊猫按类别绘制颜色的数据框条形图

来自分类Dev

绘制条形图

来自分类Dev

绘制条形图

来自分类Dev

如何在R中的同一图中显示并排条形图以及堆叠条形图?

来自分类Dev

使用定义的列定义Pandas / Matplotlib的条形图颜色

来自分类Dev

熊猫/ matplotlib条形图,其颜色由列定义

来自分类Dev

定义Matplotlib 3D条形图的颜色

来自分类Dev

如何在python中并排绘制堆叠的条形图?(最好是seaborn)

来自分类Dev

在 R 中的分组条形图 ggplot 中更改颜色

来自分类Dev

如何修改OHLC图表中条形图的默认颜色?

来自分类Dev

根据R中的值以渐变颜色显示条形图

来自分类Dev

在pyqtgraph中设置条形图的渐变颜色

来自分类Dev

条形图的颜色未显示在JBChartView中

来自分类Dev

在python中使用matplotlib绘制多个分组的条形图

来自分类Dev

如何分别指定matplotlib堆叠条形图的条形中的颜色?