【问题标题】:Control the facecolor of histograms控制直方图的 facecolor
【发布时间】:2021-08-31 13:44:48
【问题描述】:

在下面的直方图中,

import numpy as np
import matplotlib.pyplot as plt
tinter = [0.8253909999999998, 0.8804020000000001, 0.14202099999999973, 0.7727520000000005, 1.1040220000000005, 0.10714699999999944, 0.5750040000000016, 0.7876929999999973, 0.8980040000000002, 1.1478490000000008, 0.44635099999999994, 1.052067000000001, 0.4327469999999991, 0.3272960000000005, 0.26918099999999967, 0.3854459999999982, 0.1827140000000007, 0.8867760000000011, 0.7774879999999982, 0.21082900000000038, 0.6758939999999996, 0.4335760000000022, 0.6791699999999992, 0.7758439999999993, 0.15755200000000258, 0.1414289999999987, 0.36975599999999886, 0.8993549999999999, 0.6577640000000002, 1.043728999999999, 0.19952800000000082, 0.12645200000000045, 0.3454990000000002, 0.9054520000000004, 0.7165229999999987, 0.9425269999999983, 0.7159280000000052, 0.7413279999999958, 0.12669900000000212, 0.2822880000000012, 0.3690029999999993, 0.7246340000000018, 0.4718329999999966, 0.7580859999999987, 0.744059, 0.19344999999999857, 0.12031900000000206, 0.47543600000000197, 0.437542999999998, 0.44232000000000227, 0.5250109999999992, 0.17673200000000122, 0.2440649999999991, 0.31524799999999686, 0.7674680000000009, 0.7837700000000041, 1.1910290000000003, 0.14404899999999543, 0.21560399999999902, 0.19931500000000568, 0.27113699999999596, 0.728234999999998, 0.5061920000000057, 0.6459329999999994, 0.7817450000000008, 0.8265129999999985, 0.17931199999999592, 0.30208600000000274, 0.32583699999999993, 0.41771599999999864]
n, bins, patches = plt.hist(x=tinter, bins='auto', color='#0504aa',
                            alpha=0.7, rwidth=0.9,)
plt.grid(axis='y', alpha=0.35)
plt.xlabel('time [s]')
plt.ylabel('Frequency')
plt.title('Hit interval')

我想为直方图的条形设置两种不同的颜色。 有一个先例问题(this link),但解决方案将条形图的颜色设置在一个范围内,例如,一种颜色的条形图 0 到 3。

这是一个不同的问题,因为我想区分直方图中的两个区域。我不知道特定直方图的具体情况以及条形图的数量。 在上面的例子中,颜色的分离应该是 0.6:时间的左边=一种颜色的 0.6s,时间的右边=另一种颜色的 0.6。 如果无法制作两种颜色的条(在示例中,条中包含的时间值范围包括 0.6),则颜色分离应从示例中最低的条开始(时间条=0.2 ) 到时间条= 0.6 包括在内。并且,在示例中,最后四个条形(时间=0.6 秒以上)是另一种颜色。

例如,在这个例子中,nbins 的值给出了 'plt.hist()` 是:

n = array([16., 11., 10.,  3., 16.,  8.,  3.,  3.])
bins=array([0.107147  , 0.24263225, 0.3781175 , 0.51360275, 0.649088  , 0.78457325, 0.9200585 , 1.05554375, 1.191029  ])

我想要带有 n 16、11、10 和 3 的条形,它们的颜色与另一组带有 n 16、8、3,3 的条形不同,并将其用于一般直方图。

【问题讨论】:

标签: python matplotlib colors histogram


【解决方案1】:

您可以循环穿过这些条并测试它是完全在分隔线的右侧、完全在左侧还是穿过分隔线。您相应地更改栏的颜色。

当条形穿过分隔符时,条形将获得左侧区域的颜色,并且其大小会变窄以接触分隔符。可以添加条的副本,并移动右侧区域的颜色及其 x 位置。

import matplotlib.pyplot as plt
import numpy as np
from copy import copy

tinter = [0.82539, 0.8804, 0.14202, 0.77275, 1.10402, 0.10715, 0.575, 0.78769, 0.898, 1.14785, 0.44635, 1.05207, 0.43275, 0.3273, 0.26918, 0.38545, 0.18271, 0.88678, 0.77749, 0.21083, 0.67589, 0.43358, 0.67917, 0.77584, 0.15755, 0.14143, 0.36976, 0.89935, 0.65776, 1.04373, 0.19953, 0.12645, 0.3455, 0.90545, 0.71652, 0.94253, 0.71593, 0.74133, 0.1267, 0.28229, 0.369, 0.72463, 0.47183, 0.75809, 0.74406, 0.19345, 0.12032, 0.47544, 0.43754, 0.44232, 0.52501, 0.17673, 0.24406, 0.31525, 0.76747, 0.78377, 1.19103, 0.14405, 0.2156, 0.19932, 0.27114, 0.72823, 0.50619, 0.64593, 0.78175, 0.82651, 0.17931, 0.30209, 0.32584, 0.41772]
n, bins, patches = plt.hist(x=tinter, bins='auto', color='#0504aa', alpha=0.7, rwidth=0.9)
plt.grid(axis='y', alpha=0.35)
plt.xlabel('time [s]')
plt.ylabel('Frequency')
plt.title('Hit interval')
separator = 0.6
left_color = 'turquoise'
right_color = 'tomato'
for bar in patches:
    x, y = bar.get_xy()
    w = bar.get_width()
    if x > separator:
        bar.set_color(right_color)
    else:
        bar.set_color(left_color)
        if x + w > separator:
            bar.set_width(separator-x)
            new_bar = copy(bar)
            new_bar.set_x(separator)
            new_bar.set_width(x+w-separator)
            new_bar.set_color(right_color)
            plt.gca().add_patch(new_bar)
plt.show()

如果您希望包含分隔符值的完整条为一种颜色,则可以省略if x + w > separator: 下方的块。

n, bins, patches = plt.hist(x=tinter, bins='auto', color='#0504aa', alpha=0.7, rwidth=0.9)
plt.grid(axis='y', alpha=0.35)
plt.xlabel('time [s]')
plt.ylabel('Frequency')
plt.title('Hit interval')
separator = 0.6
left_color = 'turquoise'
right_color = 'tomato'
for bar in patches:
    x, y = bar.get_xy()
    w = bar.get_width()
    if x > separator:
        bar.set_color(right_color)
    else:
        bar.set_color(left_color)
plt.show()

【讨论】:

  • 这是解决方案。谢谢你。为了做出更通用的解决方案,找到分离值或绘制时间=0.6 的完整条,仅使用绿松石色。这是因为除非在脚本中设置,否则似乎很难找到分隔值。
  • 是否可以仅用绿松石色绘制时间为 0.6 的条形图?似乎很难计算分离值。
  • 将所有时间为 0.6 的条设置为绿松石色。
  • 是否可以通过观察n 和`bins` 的值来计算近似分离值?是因为我正在寻找一种通用解决方案来绘制分隔两个区域的不同直方图。
  • 可能是separation = np.median(tinter)?还是np.mean(tinter)
猜你喜欢
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 2011-06-15
  • 2011-07-14
相关资源
最近更新 更多