Matplotlib用第二组值迭代注释折线图

2024-04-19 12:40:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在matplotlib中使用一组日期值和计数构建一个线图,但是用第二组值对同一个图进行注释。到目前为止,我有:

import matplotlib.pyplot as plt

count_dates_dict = {u'Mon Feb 13 05:19:09 +0000 2012': 18, u'Mon Feb 13 05:19:04 +0000 2012': 36, u'Mon Feb 13 05:19:03 +0000 2012': 32, u'Mon Feb 13 05:18:58 +0000 2012': 14, u'Mon Feb 13 05:19:10 +0000 2012': 33, u'Mon Feb 13 05:19:02 +0000 2012': 28, u'Mon Feb 13 05:18:59 +0000 2012': 33, u'Mon Feb 13 05:19:11 +0000 2012': 38, u'Mon Feb 13 05:19:12 +0000 2012': 41, u'Mon Feb 13 05:19:08 +0000 2012': 31, u'Mon Feb 13 05:19:07 +0000 2012': 41, u'Mon Feb 13 05:19:06 +0000 2012': 46, u'Mon Feb 13 05:19:05 +0000 2012': 33, u'Mon Feb 13 05:19:00 +0000 2012': 35, u'Mon Feb 13 05:19:01 +0000 2012': 28, u'Mon Feb 13 05:19:13 +0000 2012': 13}

sorted_annotation_dates_dict = {u'Mon Feb 13 05:19:09 +0000 2012': 1, u'Mon Feb 13 05:19:03 +0000 2012': 1, u'Mon Feb 13 05:19:02 +0000 2012': 1, u'Mon Feb 13 05:18:59 +0000 2012': 1, u'Mon Feb 13 05:19:11 +0000 2012': 1, u'Mon Feb 13 05:19:12 +0000 2012': 1, u'Mon Feb 13 05:19:07 +0000 2012': 1, u'Mon Feb 13 05:19:00 +0000 2012': 1, u'Mon Feb 13 05:19:01 +0000 2012': 1, u'Mon Feb 13 05:19:13 +0000 2012': 1}

fig = plt.figure(figsize=(15,5))
ax = plt.axes()
ax2 = plt.axes()
plt.xticks(range(0, len(count_dates_dict.values())), sorted(count_dates_dict.keys(), reverse=False), rotation=90, color = 'b')

ax.plot(count_dates_dict.values(), color = 'g')

for item in sorted(sorted_annotation_dates_dict).keys():
    ax.annotate(item, xytext=('Point of Intrest'), rotation=90, arrowprops=dict(connectionstyle="arc3"), xy=(item))

plt.show()

baseplot

显然,这也会引发一个错误,即需要为注释解包的值太多。我不确定我是否完全理解注释想要什么。我假设xy=是箭头的一组点?任何帮助都会受到感激。你知道吗


Tags: matplotlibcountpltannotationkeysaxitemdict
1条回答
网友
1楼 · 发布于 2024-04-19 12:40:22

我不清楚,您希望注释发生在哪里(特别是y坐标),但我希望这就是您要寻找的。无论如何,这个例子可以帮助你实现最终目标。你知道吗

import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as dt

# create a function that reads the date string and converts into a float. 
# This function will be useful for sorting and then plotting with matplotlib
def return_float(date):
    return dt.date2num(datetime.datetime.strptime(date, '%a %b %d %H:%M:%S +0000 %Y'))

count_dates_dict = {u'Mon Feb 13 05:19:09 +0000 2012': 18, u'Mon Feb 13 05:19:04 +0000 2012': 36, u'Mon Feb 13 05:19:03 +0000 2012': 32, u'Mon Feb 13 05:18:58 +0000 2012': 14, u'Mon Feb 13 05:19:10 +0000 2012': 33, u'Mon Feb 13 05:19:02 +0000 2012': 28, u'Mon Feb 13 05:18:59 +0000 2012': 33, u'Mon Feb 13 05:19:11 +0000 2012': 38, u'Mon Feb 13 05:19:12 +0000 2012': 41, u'Mon Feb 13 05:19:08 +0000 2012': 31, u'Mon Feb 13 05:19:07 +0000 2012': 41, u'Mon Feb 13 05:19:06 +0000 2012': 46, u'Mon Feb 13 05:19:05 +0000 2012': 33, u'Mon Feb 13 05:19:00 +0000 2012': 35, u'Mon Feb 13 05:19:01 +0000 2012': 28, u'Mon Feb 13 05:19:13 +0000 2012': 13}

# convert the dict with the keys replaced with a float
count_floats_dict = {return_float(k):v for k,v in count_dates_dict.items()}

# make  sorted list of tuples. sorting is done on the first element of the tuple
count_floats_dict_sorted =  sorted(count_floats_dict.items())

# same kind of data transformation for this dict as shown above 
sorted_annotation_dates_dict = {u'Mon Feb 13 05:19:09 +0000 2012': 1, u'Mon Feb 13 05:19:03 +0000 2012': 1, u'Mon Feb 13 05:19:02 +0000 2012': 1, u'Mon Feb 13 05:18:59 +0000 2012': 1, u'Mon Feb 13 05:19:11 +0000 2012': 1, u'Mon Feb 13 05:19:12 +0000 2012': 1, u'Mon Feb 13 05:19:07 +0000 2012': 1, u'Mon Feb 13 05:19:00 +0000 2012': 1, u'Mon Feb 13 05:19:01 +0000 2012': 1, u'Mon Feb 13 05:19:13 +0000 2012': 1}
sorted_annotation_floats_dict = {return_float(k):v for k,v in sorted_annotation_dates_dict.items()}
annotation_floats_dict_sorted =  sorted(sorted_annotation_floats_dict.items())


# plotting

fig = plt.figure(figsize=(15,5))
ax = plt.axes()

# plot with x values changed to date format
ax.plot([dt.num2date(i[0]) for i in count_floats_dict_sorted], [i[1] for i in count_floats_dict_sorted])

# annotation, requires xy argument, which is the position where the arrow will point, here I choose x as the key from sorted_annotation_dates_dict and arbitrarily choose
# y = 40, xytext with textcoords = 'offset points' represent the offset coordinates for the text, here I choose (0,30), but you can modify it however you wish.

for i  in annotation_floats_dict_sorted:
    ax.annotate('poi', xy = (i[0], 40), xytext = (0, 30), textcoords='offset points', arrowprops=dict(facecolor='black', shrink=0.0005))

plt.show()

这会导致 enter image description here

相关问题 更多 >