2014-04-10 46 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