【问题标题】:Show all colors in histogram bars on top of each other without adding weights in python在直方图中显示所有颜色,而不在 python 中添加权重
【发布时间】:2020-12-06 06:15:05
【问题描述】:

我正在使用以下代码在 3 个不同的数据集 abc 上制作 5 个条形图。如何在每个栏中显示所有颜色。我不希望他们的价值加起来。例如,在第一个栏中,如果Green 的值为1Yellow3 并且Red6 我不希望最终值为10 而应该是6 但所有颜色都应该出现直到它们的最终值。我不想使用透明颜色或只使用条形轮廓。

import matplotlib.pyplot as plt
import numpy as np

a = [1, 2, 3, 4, 5]
b = [3, 4, 1, 10, 9]
c = [6, 7, 2, 4, 6]
ind = np.arange(len(a))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x=ind, height=a, width=0.35, align='center', label='Green',
       facecolor='g')
ax.bar(x=ind, height=b, width=0.35,  align='center', label='Yellow',
       facecolor='y')
ax.bar(x=ind, height=c, width=0.35,  align='center', label='Red', facecolor='r')

plt.xticks(ind, a)
plt.xlabel('Coordination Number')
plt.ylabel('Frequency')
plt.legend()
plt.show()

【问题讨论】:

  • 这两个答案对您的问题有帮助吗?

标签: python matplotlib data-visualization histogram


【解决方案1】:

“a”列的参考值为6,但不清楚是否为最大值。我把它理解为最大值并计算了组成比。 我根据结果创建了一个堆叠图。

import numpy as np
import pandas as pd
a = [1, 2, 3, 4, 5]
b = [3, 4, 1, 10, 9]
c = [6, 7, 2, 4, 6]
ind = np.arange(len(a))
df = pd.DataFrame({'a':a,'b':b,'c':c}, index=ind)
df['total'] = df.sum(axis=1)
df['max'] = df[['a','b','c']].max(axis=1)
df['aa'] = df['max']*(df['a']/df['total'])
df['bb'] = df['max']*(df['b']/df['total'])
df['cc'] = df['max']*(df['c']/df['total'])
df
    a   b   c   total   max aa  bb  cc
0   1   3   6   10  6   0.600000    1.800000    3.600000
1   2   4   7   13  7   1.076923    2.153846    3.769231
2   3   1   2   6   3   1.500000    0.500000    1.000000
3   4   10  4   18  10  2.222222    5.555556    2.222222
4   5   9   6   20  9   2.250000    4.050000    2.700000

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x=ind, height=df.loc[:,'aa'], bottom=0, width=0.35, align='center', label='Green',
       facecolor='g')
ax.bar(x=ind, height=df.loc[:,'bb'], bottom=df.loc[:,'aa'], width=0.35, align='center', label='Yellow',
       facecolor='y')
ax.bar(x=ind, height=df.loc[:,'cc'], bottom=df.loc[:,'aa']+df.loc[:,'bb'], width=0.35, align='center', label='Red', facecolor='r')

plt.xticks(ind, a)
plt.xlabel('Coordination Number')
plt.ylabel('Frequency')
plt.legend()
plt.show()

【讨论】:

  • 感谢您的帖子。代码显示了正确的峰值,但正如您所见,这改变了YellowGreen 的相应频率值。所以我希望所有颜色都能达到它们的最终值。例如,在第一个栏中,Green 应显示 1 频率,Yellow 应显示 3
  • 如果你有一列最大值为6,你可以从最大值6中减去绿色的1和黄色的3,得到红色的3,但实际数据是6。这是错误的可视化。
【解决方案2】:

如果我正确理解您的问题,您想显示从相同的零基线开始并在其相应数字下分组在一起的所有颜色条吗?

我将使用bokeh 进行绘图,因为它提供了一种简单的方法来“偏移”组中的每个条形。要改变每个条的视觉偏移量,请更改 dodge 函数的第二个参数。对于这种宽度组合,0.05 似乎是一个不错的值。

from bokeh.io import output_notebook, output_file, show
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge

output_notebook() # or output_file("chart.html") if not using Jupyter

x_axis_values = [str(x) for x in range(1, 6)]

data = {
    "Coordination Number" : x_axis_values,
    "Green"               : [1, 2, 3, 4, 5],
    "Yellow"              : [3, 4, 1, 10, 9],
    "Red"                 : [6, 7, 2, 4, 6]
}

src = ColumnDataSource(data=data)

p = figure(
    x_range=x_axis_values, y_range=(0, 10), plot_height=275,
    title="Offset Group Bar Chart", toolbar_location=None, tools="")

p.vbar(
    x=dodge('Coordination Number', -0.05, range=p.x_range),
    top='Green', width=0.2, source=src, color="#8DD3C7", legend_label="Green")

p.vbar(
    x=dodge('Coordination Number',  0.0,  range=p.x_range),
    top='Yellow', width=0.2, source=src, color="#FFD92F", legend_label="Yellow")

p.vbar(
    x=dodge('Coordination Number',  0.05, range=p.x_range),
    top='Red', width=0.2, source=src, color="#E15759", legend_label="Red")

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"
p.xaxis.axis_label = "Coordination Number"
p.yaxis.axis_label = "Frequency"

show(p)

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2021-05-16
    • 2012-01-02
    • 1970-01-01
    • 2022-01-06
    • 2019-11-20
    • 2016-10-10
    • 2017-06-29
    • 2018-04-09
    相关资源
    最近更新 更多