我的程序获取n
数据集并绘制它们的直方图。
垂直线表示数据集中出现频率最高的值。我想添加一个指示值的标签,比如从顶部开始的 20%。使用时matplotlib.pyplot.text()
我必须手动分配x
和y
值。根据数据集,文本会向上或向下移动,这是我不希望发生的。
matplot.axvline(most_common_number, linewidth=0.5, color='black')
matplot.text(most_common_number + 3, 10, str(most_common_number),
horizontalalignment='center', fontweight='bold', color='black')
我也尝试设置label
参数,matplotlib.pyplot.axvline()
但它只会添加到legend
情节的。
matplot.axvline(most_common_number, linewidth=0.5, color='black', label=str(most_common_number))
I wonder if there is a way to use percentages so the text appears n%
from the top or use a different method to label the vertical lines. Or am I doing this all wrong?
I want the x-axis ticks to be factors of 16 so I had to override the defaults. This is where the trouble began. When I save the plot to a PNG file, the x-axis looks really messed up.
But when I use show()
it works fine:
kwargs = dict(alpha=0.5, bins=37, range=(0, 304), density=False, stacked=True)
fig, ax1 = matplot.subplots()
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']
count = 0
'''
datasets = [('dataset name', ['data'])]
'''
for item in datasets:
dataset = item[1]
most_common_number = most_common(dataset)
ax1.hist(dataset, **kwargs, label=item[0], color=colors[count])
matplot.axvline(most_common_number, linewidth=0.5, color='black')
matplot.text(most_common_number + 3, 10, str(most_common_number),
horizontalalignment='center', fontweight='bold', color='black')
count += 1
#for x-axis
loc = matplotticker.MultipleLocator(base=16) # this locator puts ticks at regular intervals
ax1.xaxis.set_major_locator(loc)
#for y-axis
y_vals = ax1.get_yticks()
ax1.set_yticklabels(['{:3.1f}%'.format(x / len(datasets[0][1]) * 100) for x in y_vals])
#set title
matplot.gca().set(title='1 vs 2 vs 3')
#set subtitle
matplot.suptitle("This is a cool subtitle.", va="bottom", family="overpass")
matplot.legend()
fig = matplot.gcf()
fig.set_size_inches(16, 9)
matplot.savefig('out.png', format = 'png', dpi=120)
matplot.show()
It can be done in two ways:
Axes limits
matplotlib.pyplot.xlim and matplotlib.pyplot.ylim
ylim()
will give the max and min values of the axis. eg: (0.0, 1707.3)
matplot.text(x + matplot.xlim()[1] * 0.02 , matplot.ylim()[1] * 0.8,
str(most_common_number),,
horizontalalignment='center', fontweight='bold', color='black')
(x + matplot.xlim()[1] * 0.02
表示仅x
向右移动 2%。因为您不希望文本在它标记的垂直线上重合。
matplot.ylim()[1] * 0.8
表示位于 y 轴高度的 80%。
或者您可以使用转换参数直接指定x
和y
作为比例(例如:轴的 0.8):
matplot.text(most_common_number, 0.8,
' ' + str(most_common_number), transform=ax1.get_xaxis_transform(),
horizontalalignment='center', fontweight='bold', color='black')
这里的y = 0.8
意思是在 y 轴的 80% 高度处。
使用matplotlib.pyplot.gcf()
改变尺寸和使用自定义dpi
保存图形时(否则,文本将无法正常比例)。
gcf()
意思是“获取当前数字”。
fig = matplot.gcf()
fig.set_size_inches(16, 9)
matplot.savefig('out.png', format = 'png', dpi=120)
因此生成的图像将是 (16*120, 9*120) 或 (1920, 1080) 像素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句