简体   繁体   中英

Put the Xaxis at the Y coordinate 0 with Plotly Python

I would like to reproduce this figure on plotly,
https://i.stack.imgur.com/bxWu8.png\\ but I have a problem when I try to reproduce the horizontal line of the center, if I understood correctly only the axes can display ticks mark but it is impossible to choose a Y coordinate to position the X axis.
Here is what so far I have managed to reproduce
https://i.stack.imgur.com/7xUTk.png
Do you have an idea?
thanks in advance.

  • straight forward with plotly express
  • adjust traces texttemplate="%{text:.1%}", textposition="outside", textangle=90
  • adjust layout xaxis_type="category", yaxis_tickformat=".1%", yaxis_range=[-1.4, 1.4]
    1. xaxis_type="category" so every year is displayed
    2. yaxis_range=[-1.4, 1.4] so there is room for text
import numpy as np
import pandas as pd
import plotly.express as px

df = pd.DataFrame(
    {
        "year": np.repeat(range(2011, 2021), 2),
        "cat": np.tile(list("AB"), 10),
        "val": np.random.uniform(-1, 1, 20),
    }
)

px.bar(df, x="year", y="val", color="cat", text="val", barmode="group").update_traces(
    texttemplate="%{text:.1%}", textposition="outside", textangle=90
).update_layout(
    xaxis_type="category", yaxis_tickformat=".1%", yaxis_range=[-1.4, 1.4]
).update_yaxes(
    zeroline=True, zerolinewidth=2, zerolinecolor="black"
).update_yaxes(
    showline=True, linewidth=2, linecolor="black"
)

在此处输入图片说明

more sophisticated

  • tick marks along zero axis, tick labels at bottom of figure
  • needs two sets of traces, one for positive values and another for negative values
  • xaxis then becomes axis for tick markers
  • xaxis2 then becomes axis for tick labels
trace_opts = dict(texttemplate="%{text:.1%}", textposition="outside", textangle=90)
data_opts = dict(color="cat", text="val", barmode="group")
y_opts = {
    "tickformat": ".1%",
    "showline": True,
    "linewidth": 2,
    "linecolor": "black",
}

# positive values
figp = px.bar(
    df, x="year", y=np.where(df["val"].ge(0), df["val"], np.nan), **data_opts
).update_traces(**trace_opts)

# negative values on separate axis
fign = px.bar(
    df, x="year", y=np.where(df["val"].lt(0), df["val"], np.nan), **data_opts
).update_traces(**trace_opts, showlegend=False, yaxis="y2", xaxis="x2")

# integrate and format both sets of axis
figp.add_traces(fign.data).update_layout(
    yaxis={"domain": [0.5, 1], "range": [0, 1.5], **y_opts},
    yaxis2={"domain": [0, 0.5], "range": [-1.5, 0], **y_opts},
    xaxis=dict(
        type="category",
        ticklen=7, tickwidth=3,
        tickson="labels",
        ticks="outside",
        showticklabels=False,
    ),
    xaxis2=dict(type="category", anchor="free"),
).update_layout(xaxis_title="", xaxis2_title=figp.layout.xaxis.title)

在此处输入图片说明

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