2017-05-01 8 views
0

matplotlibにラインプロットを作成しようとしていますが、同じプロットに2番目の値セットで注釈を付けます。これまでのところ私はこれを持っている: Matplotlib 2行目の値を繰り返してラインプロットに注釈を付ける

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

アノテーションが必要な場所(特に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

+1

になり、ありがとうございました。 – secumind

関連する問題