2014-04-10 85 views
4

我有一個包含一些事件和我期待的情節和與圖表的事件細節註釋計數下面的熊貓數據框:詮釋優雅與大熊貓情節的文本不重疊

Date Time Time Zone Currency Event Importance Actual Forecast Previous Count Volume 
DateTime            
2014-04-09 00:30:00 Wed Apr 9 00:30 GMT  aud  AUD Westpac Consumer Confidence  Medium 0.3% NaN  -0.7% 198  7739 
2014-04-09 00:30:00 Wed Apr 9 00:30 GMT  aud  AUD Westpac Consumer Conf Index  Low  99.7 NaN  99.5 198  7279 
2014-04-09 01:30:00 Wed Apr 9 01:30 GMT  aud  AUD Investment Lending Low  4.4% NaN  -3.7% 172  21297 
2014-04-09 01:30:00 Wed Apr 9 01:30 GMT  aud  AUD Home Loans Medium 2.3% 1.5% 0.0% 172  22197 
2014-04-09 01:30:00 Wed Apr 9 01:30 GMT  aud  AUD Value of Loans (MoM) Low  1.9% NaN  1.6% 172  22197 

我使用下面的代碼繪製數據幀(DF)M: - 日期時間 - 由於數據幀包含重複

import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 

temp=df[df['Count'].notnull()] 
#temp=temp[temp['Importance']=="High"] 

x = temp.index 
y = temp.Count 
z = temp.Event 
g = temp.Importance 
v = temp.Volume 

fig, ax = plt.subplots(figsize=(15,8)) 
ax.plot_date(x, y, linestyle='--') 

for i in range(len(x)): 
    if g[i]=="Medium": 
     ax.annotate(z[i]+' '+'Volume: '+str(v[i]), (mdates.date2num(x[i]), y[i]), xytext=(15, 15), 
      textcoords='offset points', arrowprops=dict(arrowstyle='-|>')) 


fig.autofmt_xdate() 
plt.show() 

指標,註釋文本即將在一個重疊的方式:

overlappinglabels

是否有更好的顯示方式?

可能的解決辦法: 想想我設法隨機化的xytext得到一個確定的值陰謀

ax.annotate(z[i]+' '+'Volume: '+str(v[i]), (mdates.date2num(x[i]), y[i]), xytext=(5+randint(1,50), 5+randint(1,50)), 
      textcoords='offset points', arrowprops=dict(arrowstyle='-|>'), rotation=0) 

enter image description here

回答

1

看來你可以通過旋轉註釋文本解決了重疊的註解。這可以通過添加「旋轉= 90」

ax.annotate(z[i]+' '+'Volume: '+str(v[i]), (mdates.date2num(x[i]), y[i]), xytext=(15, 15), 
      textcoords='offset points', arrowprops=dict(arrowstyle='-|>'), rotation=90) 
+0

does not看起來像它帶走,特別是如果你使用的是靜態值旋轉overalapping文本的問題來進行。即使有隨機值,它看起來也不太好:'ax.annotate(z [i] +''+'Volume:'+ str(v [i]),(mdates.date2num(x [i]),y textcords ='偏移點',arrowprops = dict(arrowstyle =' - |>''),rotation = randint(4.5,9)* 10)'xytext =(1,1), ' – user3499276