How to remove the large left and right paddings due to “lines+markers” lines?
import numpy as np
import pandas as pd
import plotly.graph_objects as go
count = 100
df = pd.DataFrame(
{
"A": np.random.randint(10, 20, size=(count)),
"B": np.random.randint(10, 20, size=(count)),
"C": np.random.randint(100, 200, size=(count)),
},
index=[f"bar.{i}" for i in range(count)],
)
fig = go.Figure(
data=[
go.Bar(
name=df.columns[0],
orientation="v",
x=df.index,
y=df.iloc[:, 0],
text=df.iloc[:, 0],
),
go.Bar(
name=df.columns[1],
orientation="v",
x=df.index,
y=df.iloc[:, 1],
text=df.iloc[:, 1],
),
go.Scatter(
name=df.columns[2],
x=df.index,
y=df.iloc[:, 2],
text=df.iloc[:, 2],
mode="lines+markers",
),
],
layout=dict(
width=1024,
height=512,
margin=dict(l=10, r=10, t=10, b=10),
),
)
fig.update_layout(
xaxis_title=None,
yaxis_title=None,
legend_title=None,
)
fig.show()
If I don’t set the mode or reduce the data point count from 100 to 10, I can get rid of the large paddings:
This is a similar issue to the one in this question: How to remove the large top and bottom paddings due to top-to-bottom lines?
Just like this answer, we can set the range of the X axis to remove the large paddings.
import numpy as np
import pandas as pd
import plotly.graph_objects as go
count = 100
df = pd.DataFrame(
{
"A": np.random.randint(10, 20, size=(count)),
"B": np.random.randint(10, 20, size=(count)),
"C": np.random.randint(100, 200, size=(count)),
},
index=[f"bar.{i}" for i in range(count)],
)
fig = go.Figure(
data=[
go.Bar(
name=df.columns[0],
orientation="v",
x=df.index,
y=df.iloc[:, 0],
text=df.iloc[:, 0],
),
go.Bar(
name=df.columns[1],
orientation="v",
x=df.index,
y=df.iloc[:, 1],
text=df.iloc[:, 1],
),
go.Scatter(
name=df.columns[2],
x=df.index,
y=df.iloc[:, 2],
text=df.iloc[:, 2],
mode="lines+markers",
),
],
layout=dict(
width=1024,
height=512,
margin=dict(l=10, r=10, t=10, b=10),
),
)
fig.update_layout(
xaxis_title=None,
yaxis_title=None,
legend_title=None,
xaxis=dict(
range=[count - 0.5, 0 - 0.5],
),
)
fig.show()