Matplotlib:在图表上的每个点旁边显示值 [英] Matplotlib: Display value next to each point on chart
本文介绍了Matplotlib:在图表上的每个点旁边显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在图表上显示每个点旁边的值:
Is it possible to display each point's value next to it on chart diagram:
点上显示的值是:[7, 57, 121, 192, 123, 240, 546]
values = list(map(lambda x: x[0], result)) #[7, 57, 121, 192, 123, 240, 546]
labels = list(map(lambda x: x[1], result)) #['1950s', '1960s', '1970s', '1980s', '1990s', '2000s', '2010s']
plt.plot(labels, values, 'bo')
plt.show()
这是我当前用于此图表的代码.
Here's my current code for this chart.
我想知道图表上显示的每个点值,目前我只能根据 y 轴预测值.
I would like to know each point value shown on graph, currently I can only predict values based on y-axis.
推荐答案
根据您的值,这是一种使用 plt.text
Based on your values, here is one solution using plt.text
fig = plt.figure()
ax = fig.add_subplot(111)
values = [7, 57, 121, 192, 123, 240, 546]
labels = ['1950s', '1960s', '1970s', '1980s', '1990s', '2000s', '2010s']
plt.plot(range(len(labels)), values, 'bo') # Plotting data
plt.xticks(range(len(labels)), labels) # Redefining x-axis labels
for i, v in enumerate(values):
ax.text(i, v+25, "%d" %v, ha="center")
plt.ylim(-10, 595)
输出
这篇关于Matplotlib:在图表上的每个点旁边显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文