【问题标题】:Separate bar in chart图表中的单独条形图
【发布时间】:2020-10-17 20:44:14
【问题描述】:

我想在直方图的每个 bin 中显示,3 个条是分开的,这样它就不会重叠。我的代码是这样的:

face = io.imread('images/face.png')

red_chanel = face[:,:,0]
green_chanel = face[:,:,1]
blue_chanel = face[:,:,2]

red_chanel = red_chanel.astype('float')
green_chanel = green_chanel.astype('float')
blue_chanel = blue_chanel.astype('float')
face = face.astype('float')

fig, ax1 = plt.subplots(ncols = 1, figsize = (20, 5))

hstred=exposure.histogram(red_chanel, nbins=28)
hstgreen=exposure.histogram(green_chanel, nbins=28)
hstblue=exposure.histogram(blue_chanel, nbins=28)

ax1.bar(list(range(28)), hstred[0], align='edge')
ax1.bar(list(range(28)), hstgreen[0], align='edge')
ax1.bar(list(range(28)), hstblue[0], align='edge')

plt.show()

如何分隔条形?

【问题讨论】:

  • 能否在文本表单中添加代码?
  • 是的,我刚刚添加了它
  • 添加了一个答案。请让我知道这对你有没有用。如果确实如此,请考虑接受/勾选答案。

标签: python matplotlib charts histogram


【解决方案1】:

我认为您可以移动第二个和第三个条形图的 x 轴并稍微使用条形图 width。最后把xticks改成。

import numpy as np
ax1.bar(np.arange(28), hstred[0], align='edge', width=0.3)
#shifting the xaxis
ax1.bar(np.arange(28)+0.3, hstgreen[0], align='edge', width=0.3)
ax1.bar(np.arange(28)+0.6, hstblue[0], align='edge', width=0.3)
plt.xticks(np.arange(0,28)+0.3, np.arange(0,28)) #resetting the ticks

这是一个例子:

x1 = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 5, 6]
y2 = [4, 4, 2, 2, 2]
y3 = [3, 4, 6, 7, 8]

fig,ax = plt.subplots()
ax.bar(x1,y1,width=0.3)
ax.bar(np.array(x1)+0.3,y2,width=0.3) 
ax.bar(np.array(x1)+0.6,y3,width=0.3)
plt.xticks(np.arange(0,6)+0.3, np.arange(0,6))
plt.show()

输出

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2020-02-10
    • 2020-11-28
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多