如何为折线图上的点写入精确数值?

huangapple go评论115阅读模式
英文:

How to write exact values for points on line graphs?

问题

  1. import matplotlib.pyplot as plt
  2. objects = ('A', 'B', 'C')
  3. avgA, avgB, avgC = 0.009990256984352774, 0.0014206548643907065, 0.055161861569464204
  4. performance = [avgA, avgB, avgC]
  5. exact = plt.plot(objects, performance, alpha=0.5, color='purple')
  6. plt.xlabel('Compression Method')
  7. plt.ylabel('Average Distance b/w Uncompressed & Compressed Point')
  8. plt.title('Evaluation of Different Compression Methods - Averages')
  9. plt.tight_layout()
  10. plt.show()
  1. X轴标签应为A、B和C,而不是0.0、1.0和2.0。Y轴上的值是正确的。

  2. 如何在线图上显示A/B/C的确切值?例如,x轴上的A对应于y轴上的0.00999,但在图上没有显示确切的值。就像在柱状图上我们可以在柱子的顶部写上值一样。我们可以为线图这样做吗?

  3. 此外,如何改善刻度?当前,我的图显示的y轴值范围从0.0到0.5,但我想使其更精确。

英文:
  1. import matplotlib.pyplot as plt
  2. objects = ('A', 'B', 'C')
  3. avgA, avgB, avgC = 0.009990256984352774, 0.0014206548643907065, 0.055161861569464204
  4. performance = [avgA, avgB, avgC]
  5. exact = plt.plot(performance, alpha=0.5, color= 'purple')
  6. plt.xlabel('Compression Method')
  7. plt.ylabel('Average Distance b/w Uncompressed & Compressed Point')
  8. plt.title('Evaluation of Different Compression Methods - Averages')
  9. plt.tight_layout()
  10. plt.show()

There are 3 issues with my graph:

  1. I'd like the X axis labels to be A, B & C instead of 0.0 ,1.0 & 2.0. The values on y-axis are correct.

  2. How can I display the exact value on my line graph for A/B/C? For instance, A on the x axis corresponds to 0.00999 on the y axis but on the graph the exact value isn't written anywhere. Like in bar charts we can write values on top of the bar. Can we do so for line graphs?

  3. Also, how can I improve the scale? Currently, my graph displays y axis values from 0.0 to 0.5 but I want to make it more precise.
    如何为折线图上的点写入精确数值?

答案1

得分: 1

要设置x轴刻度,最好是将plot函数的第一个参数设置为objects。要设置更多的y轴刻度,可以使用MultipleLocator来指定主要刻度和次要刻度之间的距离(主要刻度显示数字)。

要向图表添加文本,只需调用plt.annotate('text', xy=(x, y)),其中x取值为0、1、2,因为x只是标签。y是通常的y值。
您可以添加许多选项来定位文本,包括箭头、对齐等。请参阅文档

  1. import matplotlib.pyplot as plt
  2. from matplotlib.ticker import MultipleLocator
  3. avgA, avgB, avgC = 0.009990256984352774, 0.0014206548643907065, 0.055161861569464204
  4. objects = ('A', 'B', 'C')
  5. performance = [avgA, avgB, avgC]
  6. plt.plot(objects, performance, alpha=0.5, color='purple')
  7. plt.plot(objects, performance, color='dodgerblue', marker='o')
  8. ax = plt.gca()
  9. ax.yaxis.set_major_locator(MultipleLocator(0.005))
  10. ax.yaxis.set_minor_locator(MultipleLocator(0.001))
  11. for i, avg in enumerate(performance):
  12. plt.annotate('%0.5f' % avg, xy=(i, avg), color='dodgerblue', xytext=(7, 2), textcoords='offset points')
  13. plt.xlim(-0.1, 2.35) # 设置x轴限制以为文本腾出空间
  14. plt.xlabel('压缩方法')
  15. plt.ylabel('未压缩点与压缩点之间的平均距离')
  16. plt.title('不同压缩方法的评估 - 平均值')
  17. plt.tight_layout()
  18. plt.show()

如何为折线图上的点写入精确数值?

英文:

To set the xticks, best to just call plot with objects as its first parameter. To set more yticks, MultipleLocator can be used to indicate the distance between the major and the minor ticks (the major ticks display a number).

To add text to the plot, just call plt.annotate('text', xy=(x,y)) where the x goes 0, 1, 2 as the x are just labels. The y is the usual y-value.
You can add many option to position the text, with or without arrow, aligning etc. See the documentation.

  1. import matplotlib.pyplot as plt
  2. from matplotlib.ticker import MultipleLocator
  3. avgA, avgB, avgC = 0.009990256984352774, 0.0014206548643907065, 0.055161861569464204
  4. objects = ('A', 'B', 'C')
  5. performance = [avgA, avgB, avgC]
  6. plt.plot(objects, performance, alpha=0.5, color= 'purple')
  7. plt.plot(objects, performance, color= 'dodgerblue', marker='o')
  8. ax = plt.gca()
  9. ax.yaxis.set_major_locator(MultipleLocator(0.005))
  10. ax.yaxis.set_minor_locator(MultipleLocator(0.001))
  11. for i, avg in enumerate(performance):
  12. plt.annotate('%0.5f' % avg, xy=(i, avg), color='dodgerblue', xytext=(7, 2), textcoords='offset points')
  13. plt.xlim(-0.1, 2.35) # set xlims to make place for the text
  14. plt.xlabel('Compression Method')
  15. plt.ylabel('Average Distance b/w Uncompressed & Compressed Point')
  16. plt.title('Evaluation of Different Compression Methods - Averages')
  17. plt.tight_layout()
  18. plt.show()

如何为折线图上的点写入精确数值?

huangapple
  • 本文由 发表于 2020年1月4日 00:48:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定