简体   繁体   中英

Add text in Plotly.express

I have this dataframe

df = pd.DataFrame({'time':[1,2,3,1,2,3],
              'department':['A','A','A','B','B','B'],
               'revenue':[10,11,12,13,14,15]})

I plotted it using

import plotly.express as px
fig = px.line(df,x=df.time,y=df.revenue,color=df.department)
fig.update_traces(mode='lines + markers',
             hovertemplate = 'Revenue: %{y:.d} <br>') 
             
fig.show()

I was able to have the 'revenue' showing up on the hover-template box but I also want to have the 'department' showing up too. However, I'm not really sure how to do this. Could somebody help me out please

You can add hover_name="department" as shown:

fig = px.line(df,x="time",y="revenue",color="department",hover_name="department")

By default, the data frame column is displayed in the hover box, so there is no need to play around with the template.

# fig.update_traces(mode='lines + markers',
             hovertemplate = 'Revenue: %{y:.d} <br>') 

fig.update_traces(mode='lines + markers')

在此处输入图像描述

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