使用 matplotlib 注释图 - 在图中显示值 [英] Annotate a plot using matplotlib - showing values in the plot

查看:41
本文介绍了使用 matplotlib 注释图 - 在图中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:一个带有字符串的列表(liste_string)和一个带有数字的列表(liste_summe).包含字符串的列表包含日期值作为字符串.我想在图中显示 liste_summe 中的值,但它不起作用.

 plt.subplots_adjust(left=None, bottom=0.4, right=None, top=None, wspace=None, hspace=None)#Grösse des Fenster einrichtenplt.annotate(liste_summe,xy =(liste_summe,liste_datum))plt.xticks(rotation = 90)#Schrägstellen der Beschriftung der x-Achse um x Gradplt.ylim([y_min,y_max])plt.plot(liste_string,liste_summe)plt.show()

执行此代码时出现此错误:

TypeError:float()参数必须是字符串或数字,而不是列表"

有人有想法吗?

解决方案

plt.annotate 无法处理列表.如

I have two lists: one with strings in it (liste_string) and one list with numbers in it (liste_summe). The list with the strings contains Date-Values as a string. I want to show the values from liste_summe in the plot, but it doesn't work.

 plt.subplots_adjust(left=None, bottom=0.4, right=None, top=None, wspace=None, hspace=None)#Grösse des Fenster einrichten
plt.annotate(liste_summe, xy = (liste_summe, liste_datum))
plt.xticks(rotation = 90)#Schrägstellen der Beschriftung der x-Achse um x Grad
plt.ylim([y_min, y_max])
plt.plot(liste_string, liste_summe)
plt.show()

When I execute this code I get this Error:

TypeError: float() argument must be a string or a number, not 'list'

Does any one has an idea?

解决方案

plt.annotate cannot work with lists. As seen in ♠the documentation it wants a string as its fist argument and a coordinate pair as it's second argument. Since the x value is the date, the first entry in the coordinate tuple must be the date (not the inverse). In order to annotate all points, you can use a loop; one call to plt.annotate per point to annotate.

import matplotlib.pyplot as plt

liste_summe = [1,2,3,1,4]
liste_datum = ["2017-02-05", "2017-02-06", "2017-02-09", "2017-02-12", "2017-02-24"]
liste_string = liste_datum

plt.figure()
plt.subplots_adjust(bottom=0.4)
for s, d in zip(liste_summe, liste_datum):
    plt.annotate(d, xy = (d,s ))
plt.xticks(rotation = 90)

plt.plot_date(liste_string, liste_summe)
plt.show()

这篇关于使用 matplotlib 注释图 - 在图中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆