Python 如何使用plotly Express添加第二个Y轴?

Python 如何使用plotly Express添加第二个Y轴?,python,plotly,plotly-express,Python,Plotly,Plotly Express,首先,我将向您展示代码。我在收集数据方面没有问题,但更重要的是要以正确的方式呈现数据 #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_in

首先,我将向您展示代码。我在收集数据方面没有问题,但更重要的是要以正确的方式呈现数据

#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()
#获取评论日期
df[“日期”]=df[“日期”].dt.Date
#获取每天提到每个股票代码的评论数
dfDay=df.groupby([“Ticker”,“Date”]).sum().reset_index()
dfDay=dfDay[[“股票代码”、“日期”、“提及”]]
dfDay[“Date”]=pd.to_datetime(dfDay[“Date”])
dfGMEm=dfDay[dfDay[“股票代码”]=“GME”]
以yf形式导入yf财务
dfGMEp=yf.下载(“GME”,start=“2020-05-01”,end=“2021-05-15”,interval=“1d”).重置索引()
dfGMEp[“结算金额”]=dfGMEp[“结算”]
dfCombined=pd.concat([dfGMEm,dfGMEp])
将plotly.express导入为px
图=px.行(dfCombined,x=“Date”,y=[“提及”,“结束”],title='GME Erwähnungen',
颜色离散序列=[“rgb(229,81,39)”,“rgb(118213,232)”)
图:更新布局图(title=“Erwähnungen-Preis”+”
2018年8月-2021年5月),titlefont=dict(color='rgb(229,81,39)',size=20),绘图(blot)'bgcolor='rgb(32,36,44)',纸张(32,36,44)' 图1.update_xaxes(title_text=”“,color='white',showgrid=False,tickfont=dict(size=10)) 图:更新(title_text=“Erwähnungen”,secondary_y=False,color='white',showgrid=False,titlefont=dict(size=20),gridcolor=“rgb(228,49,34)”) 图1.2.2.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1( 图例=dict( title=dict(text=”“,font=dict(color='white')), x=0.85,y=1.15, 口述( 颜色为白色, 尺寸=15 ) ) ) 图更新_轨迹(线=dict(宽度=3)) 对于范围内的i(0,len(图数据)): fig.data[i].hovertemplate=“%%x}
${y:.0f}” 图2(图3)
创建的绘图仅显示Y轴上的提及次数。我想找到一种方法,得到代表股票价格的第二个Y轴。 有人知道解决我问题的方法吗?

我可能错了,但这似乎是个坏主意

2轴图表很容易受到程序员的偏见影响,对于很多人来说很难阅读 杂乱无章的东西让你很难分辨任何东西 趋势、模式 并了解图表的主题和故事

这里有一个例子

他用这些图表做出了听起来荒谬的疯狂关联。 是的,我是故意的 还有这个博客


这提供了很多好的解决方案 他们也有这个有点恼人的视频 针对两个轴解释其原因 它站得住脚
但它的冗长。你说得对。读了第二篇文章后,我理解了双轴问题。我想我会坚持用一个y轴做一个并排的图表!非常感谢,即使你没有解决我遇到的问题,你仍然给出了最好的答案!