Many of us have to write reports or make presentations with visualizations. The challenge sometimes isn’t the underlying data or the visualization itself, but the corporate design. In my case, the corporate design of the organization is based on an excel template for diagrams, with all its disadvantages. My goal is to automate most of it with plotly and convince my academic organization to move to plotly to a certain extent. I simply love plotly and its capabilities!
Therefore I would like to share things I couldn’t find elsewhere and others also might search for in future. Or simply raise questions
A border around the plot.
If you can’t just style the axis and mirror it, because you need different line width just add a shape (line/rectangle)to the figure. It took me a while to figure it out, so I feel like sharing might be worth it.
df = px.data.gapminder().query("continent=='Americas'")
ls_first5 = df.country.unique()[:5]
df = df.loc[df.country.isin(ls_first5)]
fig = px.scatter(df, x="year", y="lifeExp", color= 'country')
fig.add_shape(
# Rectangle with reference to the plot
type="rect",
xref="paper",
yref="paper",
x0=0,
y0=0,
x1=1.0,
y1=1.0,
line=dict(
color="black",
width=1,
)
)
fig.update_layout(plot_bgcolor ='white')
fig.update_xaxes(showline=True, linewidth=1.5, linecolor='black',
gridcolor='black', gridwidth=1,
zeroline=True, zerolinewidth=1.5, zerolinecolor='black',
)
fig.update_yaxes(showline=True, linewidth=1.5, linecolor='black',
gridcolor='black', gridwidth=1,
zeroline=True, zerolinewidth=1.5, zerolinecolor='black',
)
fig.show()
If you want to gain even more control over the axis just use lines instead of the rectangle.
fig.add_shape(
# Line with reference to the plot
type="line",
xref="paper",
yref="paper",
x0=0,
y0=1.0,
x1=1.0,
y1=1.0,
line=dict(
color="black",
width=1,
)
)
fig.add_shape(
# Line with reference to the plot
type="line",
xref="paper",
yref="paper",
x0=1.0,
y0=1.0,
x1=1.0,
y1=0,
line=dict(
color="black",
width=1,
)
)