【问题标题】:Plotting the data using the matplotlib and coloring the group histograms with the same color使用 matplotlib 绘制数据并使用相同颜色对组直方图进行着色
【发布时间】:2020-11-07 18:57:29
【问题描述】:

我正在尝试使用 matplotlib 库绘制图表。这是我的代码。

import numpy as np
import matplotlib.pyplot as plt
data = [[206.6, 735.4, 427.9, 175.2,384.4],
[487.5, 273.7, 742.6, 159.5,144],
[613.4, 0, 294.9, 0,0]]
X = np.arange(5)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
ax.legend(labels=['Group-1', 'Group-2','Group-3','Group-4','Group-5'])

我希望上述图表组具有相同的颜色。例如,Group-1 直方图应该是 Red,Group-2 直方图应该是 Blue,Group-3 直方图应该是 Orange,等等。我怎样才能使用上面的代码得到它

【问题讨论】:

    标签: python matplotlib graph data-science


    【解决方案1】:

    以下内容将按照我认为您从代码开始的要求进行。这个请求对我来说似乎有点奇怪,因为您可以在 x 轴上标记图组,因此颜色提供了一种标记每个系列的明显方式。

    import numpy as np
    import matplotlib.pyplot as plt
    data = [[206.6, 735.4, 427.9, 175.2,384.4],
    [487.5, 273.7, 742.6, 159.5,144],
    [613.4, 0, 294.9, 0,0]]
    X = np.arange(5)
    col_list = ['red','blue','orange','green','cyan']
    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])
    handles = ax.bar(X + 0.00, data[0], color = col_list, width = 0.25)
    ax.bar(X + 0.25, data[1], color = col_list, width = 0.25)
    ax.bar(X + 0.50, data[2], color = col_list, width = 0.25)
    ax.legend(labels=['Group-1','Group-2','Group-3','Group-4', 'Group-5'],
               handles=handles)
    plt.show()
    

    给予:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多