自动放置注释气泡

时间:2020-07-04 21:16:18

标签: python python-3.x pandas matplotlib annotations

我有以下代码片段:

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,这使“气泡”变得混乱,因为它们在某些重叠的位置上很难阅读。有什么办法可以将其“自动放置”,以便它们不会重叠。例如某些“气泡”以不重叠的方式位于绘图线的上方和下方。

2 个答案:

答案 0 :(得分: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()

以上代码的工作方式:

enter image description here

答案 1 :(得分: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