【问题标题】:Wrong positions of group bars in plot图中组条的位置错误
【发布时间】:2017-06-05 06:57:45
【问题描述】:

尝试使用MatPlotLib 绘制组条形图,但我的条形图位置错误。

所以,我有一个数据:

groups = [
    np.array([ group1.count(0), group1.count(1) ]),
    np.array([ group2.count(0), group2.count(1) ]),
] 

group[[1, 3], [0, 5]],这意味着每个组有两种颜色。第一组只有 1 人选择第一种颜色,其他 3 人选择第二种颜色。我正在尝试绘制它,但条形图的位置错误:3 从第一组移到第二组,依此类推。

我的错误在哪里,为什么会这样?

绘制数据的代码:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

def draw(groups):
    group_labels = ['G1', 'G2']
    num_items = len(group_labels)
    ind = np.arange(num_items)
    margin = 0.05
    width = (1.-2.*margin)/num_items
    colors = ['#f5abb5', '#2fc2ef']

    s = plt.subplot(1,1,1)
    for num, vals in enumerate(groups):
        xdata = ind+margin+(num*width)
        plt.bar(xdata, vals, width, color=colors[num])
    s.set_xticks(ind+0.5)
    s.set_xticklabels(group_labels)


draw(groups)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    你可以转置输入数组

    groups = numpy.array([[1, 3], [0, 5]]).T
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2015-12-21
    • 2016-02-24
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多