简体   繁体   中英

How can I add a second Y axis with plotly Express?

First I will show you the code. I dont have problems gathering data, but more with presenting them the right way.

#Get date of comments
df["Date"] = df["Date"].dt.date


#Get number of comments mentioning each ticker on each day
dfDay = df.groupby(["Ticker","Date"]).sum().reset_index()
dfDay = dfDay[["Ticker", "Date", "Mentions"]]
dfDay["Date"] = pd.to_datetime(dfDay["Date"])

dfGMEm = dfDay[dfDay["Ticker"]== "GME"]


import yfinance as yf

dfGMEp = yf.download("GME", start="2020-05-01", end="2021-05-15",interval="1d").reset_index()
dfGMEp["CloseAmount"] = dfGMEp["Close"]

dfCombined = pd.concat([dfGMEm, dfGMEp])


import plotly.express as px


fig = px.line(dfCombined, x="Date", y=["Mentions", "Close"], title='GME Erwähnungen',
             color_discrete_sequence=["rgb(229, 81, 39)","rgb(118, 213, 232)" ])



fig.update_layout(title="Erwähnungen - Preis"+"</b><br>Aug 2018 - Mai 2021", titlefont=dict(color='rgb(229, 81, 39)', size=20), plot_bgcolor='rgb(32,36,44)', paper_bgcolor='rgb(32,36,44)')
fig.update_xaxes(title_text="",color='white', showgrid=False, tickfont=dict(size=10))
fig.update_yaxes(title_text="Erwähnungen", secondary_y=False, color='white', showgrid=False, titlefont=dict(size=20),gridcolor="rgb(228,49,34)")
fig.update_layout(
    legend=dict(
        title=dict(text="",font=dict(color='white')),
        x=.85, y=1.15,
        font=dict(
            color='white',
            size=15
        )
    )
)
fig.update_traces(line=dict(width=3))
for i in range(0, len(fig.data)):
    fig.data[i].hovertemplate = "<b>%{x}</b><br>$%{y:.0f}<extra></extra>"


fig.show()

The Plot created just shows the mentions count on the Y axis. I want to find a way to get a second Y axis representing the Price of the stock. Does anyone know a way to solve my problem?

I may be wrong here-- but it seems like a bad idea..

2-axis charts are really vulnerable to bias on the programmers part, hard to read for a large amount of people and the clutter makes it harder for you to distinguish any trends, patterns and to take in the theme and story of the chart.

here's an example http://www.tylervigen.com/spurious-correlations

he uses these charts to make crazy correlations that sound ridiculous. Yes did it on purpose and there's this blog


That provides a lot of nice solutions to think about they also have this slightly annoying video explaining their reasons against 2 axes it holds up but its long winded. here https://blog.datawrapper.de/dualaxis/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM