【发布时间】: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 秒以上)是另一种颜色。
例如,在这个例子中,n 和 bins 的值给出了 '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