简体   繁体   中英

Plotly: How to change ticks in a subplot

I created 7 subplots using plotly.subplot.make_subplots() .
Each plot is obtained with fig.add_trace(go.Scatter()) .
Usually to change ticks along one axis you could use

fig.update_layout(
    xaxis=dict(
        tickmode='array',
        tickvals= [...]  #some intervals,
        ticktext= [...]  #some text to show along the axes
        )
    )

But in this case, since fig is the whole figure created by make_subplots() , this solution is wrong.
How can I refer only to one subplot at a time?
Is there any parameter I can set in go.Scatter() that does this?
I looked up the documentation but so far I haven't found what I'm looking for.

First, the best way to understand the graph structure is to view fig.data, where each subplot has its attributes set in dictionary form. For the axes, the subplots are numbered sequentially as y, y2, y3. If you want to set this in any range, set it in the list.

from plotly.subplots import make_subplots
import plotly.graph_objects as go

df = px.data.iris()

fig = make_subplots(rows=1, cols=3)

for i,s in enumerate(df.species.unique()):
    dfs = df.query('species == @s')
    fig.add_trace(go.Scatter(
        mode='markers',
        x=dfs['sepal_width'],
        y=dfs['sepal_length'],
        name=s),
    row=1, col=i+1
)

fig.update_layout(yaxis=dict(tickvals=[5.1, 5.9, 6.3, 7.5]),
                 yaxis2=dict(tickvals=[5.0, 6.0, 7.0, 8.5]),
                 yaxis3=dict(tickvals=[5.0, 6.0, 6.5, 7.0]),
                 )

fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()

在此处输入图像描述

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