Matplotlib通过迭代第二组值来注释线图

时间:2017-05-01 22:34:19

标签: python matplotlib plot

我正在尝试使用一组日期值和计数在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 =需要为箭头设置一些点?任何帮助总是受到赞赏。

1 个答案:

答案 0 :(得分:1)

我不清楚你想要注释发生在哪里(尤其是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