python – 使用不重叠的文本优雅地注释pandas plot

python – 使用不重叠的文本优雅地注释pandas plot,第1张

概述我有下面的pandas数据框,其中包含一些事件和计数,我希望在图表中绘制和注释事件详细信息: Date Time Time Zone Currency Event Importance Actual Forecast Previous Count VolumeDateTime 我有下面的pandas数据框,其中包含一些事件和计数,我希望在图表中绘制和注释事件详细信息:

Date    Time    Time Zone   Currency    Event   importance  Actual  Forecast    PrevIoUs    Count   VolumeDateTime                                            2014-04-09 00:30:00  Wed Apr 9   00:30   GMT     aud     AUD Westpac Consumer ConfIDence     Medium  0.3%    NaN     -0.7%   198     77392014-04-09 00:30:00  Wed Apr 9   00:30   GMT     aud     AUD Westpac Consumer Conf Index     Low     99.7    NaN     99.5    198     72792014-04-09 01:30:00  Wed Apr 9   01:30   GMT     aud     AUD Investment Lending  Low     4.4%    NaN     -3.7%   172     212972014-04-09 01:30:00  Wed Apr 9   01:30   GMT     aud     AUD Home Loans  Medium  2.3%    1.5%    0.0%    172     221972014-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):

import matplotlib.pyplot as pltimport matplotlib.dates as mdatestemp=df[df['Count'].notnull()]#temp=temp[temp['importance']=="High"]x = temp.indexy = temp.Countz = temp.Eventg = temp.importancev = temp.Volumefig,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()

由于数据框包含重复 – 日期时间 – 索引,带注释的文本将以重叠的方式出现:

有没有更好的显示方式?

可能的方法:
我想通过随机化xytext值来设法得到一个好的情节

ax.annotate(z[i]+' '+'Volume: '+str(v[i]),xytext=(5+randint(1,50),5+randint(1,50)),arrowprops=dict(arrowstyle='-|>'),rotation=0)
解决方法 您似乎可以通过旋转注释文本来解决重叠注释.这可以通过添加’rotation = 90’来完成

ax.annotate(z[i]+' '+'Volume: '+str(v[i]),rotation=90)
总结

以上是内存溢出为你收集整理的python – 使用不重叠的文本优雅地注释pandas plot全部内容,希望文章能够帮你解决python – 使用不重叠的文本优雅地注释pandas plot所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1197060.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存