gpt4 book ai didi

python - 对于直方图,如何保留所有指定的 bin-ticks 沿 x 轴并仅显示指定的 bin-ticks 的数字标签?

转载 作者:行者123 更新时间:2023-11-30 22:41:37 26 4
gpt4 key购买 nike

我有一个histogram with centered bin-ticks

如图所示,每个 bin-tick 的编号标签彼此非常接近,以至于数字不像我希望的那样清晰。我想在我的图中保留居中的垃圾箱标记,但标记每个其他垃圾箱标记的值。

举个例子,第一个 bin-tick 是 2.5,每个 bin-width 是 5,所以下一个 bin-tick 是 7.5,之后的 bin-tick 是 12.5,等等。我想要 x 上的刻度线-axis 在 2.5、7.5、12.5 等处可见,但我希望仅数字标签 2.5、12.5 等在 x 轴上可见。

我怎样才能做到这一点?下面发布了创建与我的类似的直方图的代码(取决于值的随机模块)。

PS - 我可以减少 bin 的数量以适应 x 轴上的所有编号标签,但我更愿意保持 bin 的数量不变,因为我将此基本情况应用于更大的数据集指定的 bin 编号。

import random
a = 48
b = 8
randg = []
for index in range(100):
randg.append( random.gauss(a,b) )
data = sorted(randg)

small = 0
big = 100
numbins = 20
binwide = (big - small) / numbins
binleft = [] # left boundaries of bins
for index in range(numbins):
binleft.append(small + index * binwide)
binbounds = [val for val in binleft]
binbounds.append(big) # don't forget right boundary of last bin

binmids = [] # midpt of bins (for ticks)
for index in range(len(binbounds)-1):
binmids.append( binwide/2 + index * binwide )

import matplotlib.pyplot as plt
plt.hist(data, bins = binbounds)
plt.xticks(binmids)
plt.show()

最佳答案

如果您也愿意忽略每秒的刻度,一个简单的解决方案是将刻度设置为仅 binmids 列表的每隔一个条目。

plt.xticks(binmids[::2]) 

enter image description here

如果这不是一个选项,您可以将刻度设置为完整的 binmids 列表,然后将所有其他刻度标签设置为空字符串 ("")。

plt.xticks(binmids)
ticklabels = [ binmids[i] if i % 2 == 0 else "" for i in range(len(binmids)) ]
plt.gca().set_xticklabels( ticklabels )

enter image description here

关于python - 对于直方图,如何保留所有指定的 bin-ticks 沿 x 轴并仅显示指定的 bin-ticks 的数字标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com