自动放置注释气泡

5

我有以下代码片段:

data.plot(y='Close', ax = ax)
newdates = exceptthursday.loc[start:end]
for anotate in (newdates.index + BDay()).strftime('%Y-%m-%d'):
    ax.annotate('holliday', xy=(anotate, data['Close'].loc[anotate]),  xycoords='data',
                xytext=(-30, 40), textcoords='offset points',
                size=13, ha='center', va="baseline",
                bbox=dict(boxstyle="round", alpha=0.1),
                arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1)); 

这将生成一个类似于这样的图表: enter image description here 如您所见,我已经明确提到了xytext,这会使得 "气泡" 在某些位置重叠,这使得阅读变得困难。是否有任何方法可以自动放置它们,以便它们不重叠。例如,一些 "气泡" 位于绘图线上方和下方,以使它们不重叠。

3
你是否已经查看了这些问题中提出的解决方案?1, 2, 3 - Lith
你能发布一下样本数据框吗? - bigbounty
@bigbounty 我使用了 web.DataReader('fb', 'yahoo') 的数据。这是来自于 from pandas_datareader import data as web - Slartibartfast
2个回答

5

根据我的理解,自动布局是指当您放大缩小时,数据和UI将自动调整,因为即使您将气泡放在图形线的上方和下方,由于大量数据点不可忽略,您无法避免重叠。我使用了plotly库,因为matplotlib有限制。我选择了英国假期。您可以根据需要进行更改。

import plotly.graph_objects as go
import plotly.express as px
from pandas_datareader import data as web
import holidays

data = web.DataReader('fb', 'yahoo')
uk_holidays = holidays.UnitedKingdom()
data["is_holiday"] = [True if i in uk_holidays else False for i in data.index]
data["Date"] = data.index
data.reset_index(drop=True, inplace=True)

fig = px.line(data, x='Date', y='Close')

fig.update_xaxes(
    rangeslider_visible=True,
    rangeselector=dict(
        buttons=list([
            dict(count=1, label="1m", step="month", stepmode="backward"),
            dict(count=6, label="6m", step="month", stepmode="backward"),
            dict(count=1, label="YTD", step="year", stepmode="todate"),
            dict(count=1, label="1y", step="year", stepmode="backward"),
            dict(step="all")
        ])
    )
)


for close, date in data[data["is_holiday"] == True][["Close","Date"]].itertuples(index=False):
    fig.add_annotation(
        x=date.date(),
        y=close,
        xref="x",
        yref="y",
        text="Holiday",
        showarrow=True,
        font=dict(
            family="Courier New, monospace",
            size=16,
            color="#ffffff"
            ),
        align="center",
        arrowhead=2,
        arrowsize=1,
        arrowwidth=2,
        arrowcolor="#636363",
        ax=20,
        ay=-30,
        bordercolor="#c7c7c7",
        borderwidth=2,
        borderpad=4,
        bgcolor="#ff7f0e",
        opacity=0.8
        )
fig.update_layout(title_text='Trend Analysis with Holiday', title_x=0.5,showlegend=False)
fig.show()

以上代码的工作原理:

在此输入图片描述


2

由于使用的假期数据量较少,注释重叠度似乎不太有效,但是问题的要点是可以通过根据索引变化注释位置来稍微改进。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from pandas_datareader import data as web
from pandas.tseries.holiday import USFederalHolidayCalendar as calendar

data = web.DataReader('fb', 'yahoo')
cal = calendar()
holidays = cal.holidays(start=data.index.min(), end=data.index.max())
data['Holiday'] = data.index.isin(holidays)
holiday = data[data['Holiday'] == True]

fig = plt.figure(figsize=(16,6))
ax = fig.add_subplot(111)

ax.plot(data.index, data.Close)

for i,x,y in zip(range(len(holiday)),holiday.index, holiday.Close):
    if i % 2 == 0: 
        ax.annotate('holliday', xy=(x,y),  xycoords='data',
                    xytext=(-30, 40), textcoords='offset points',
                    size=13, ha='center', va="baseline",
                    bbox=dict(boxstyle="round", alpha=0.1),
                    arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1))
    else:
        ax.annotate('holliday', xy=(x,y),  xycoords='data',
                xytext=(30, -40), textcoords='offset points',
                size=13, ha='center', va="baseline",
                bbox=dict(boxstyle="round", alpha=0.1),
                arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1))


ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=None, interval=3, tz=None))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
ax.tick_params(axis='x', labelrotation=45)

enter image description here


请问如何将x轴更改为日期?同时,我遇到了“TypeError: float() argument must be a string or a number, not 'Timestamp'”错误,请问该如何解决? - Slartibartfast
X轴已经被修改以显示日期,并且代码和图像已被替换。 - r-beginners

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接