plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.26k stars 2.55k forks source link

Empty String on axis ticks changes plot size #3099

Open JWagnerDE opened 3 years ago

JWagnerDE commented 3 years ago

If setting the tick mode to 'array' on y or x axis and giving a ticktext with one ore more elements as empty string, the size of the plot will change.

Normal Bug
test test

This effect can be more drastically if using subplots. One time the plot got shrunk down to ~20% in size.

To reproduce just replace one of the strings 'Text *'in line 17 with an empty string "".

Code to reproduce

import numpy as np

import plotly.graph_objects as go

np.random.seed(42)

df = pd.DataFrame(np.random.randint(0,4, 100))

fig = go.Figure()

fig.update_layout(title_text='Test plot', height=900, width=1600)

fig.add_trace(go.Scatter(x=df.index, y=df[0]))

fig.update_yaxes(dict(tickmode = 'array', tickvals = [0,1,2,3], ticktext=['', 'Text 1', 'Text 2', 'Text 3']))

fig.write_image('test' + '.png')
kvanderwijst commented 2 years ago

This problem becomes even more problematic when using subplots:

Normal Bug
image image

Code used:

fig = make_subplots(1,2)
fig.add_scatter(x=[1,2,3], y=[3, 8, 1], row=1, col=1)
fig.add_scatter(x=[1,2,3], y=[5, 0, 10], row=1, col=2)
fig.update_xaxes(tickmode='array', tickvals=[1,2,3], ticktext=['A', '', 'C'], col=2)
mathijsvdven commented 2 years ago

I am experiencing the same problem and ended up using a workaround. Using a newline character ('\n') instead of an empty string ('') seems to solve the formatting issue.

Tickvals: [-2.0, -1.699, -1.523, -1.398, -1.301, -1.222, -1.155, -1.097, -1.046, -1.0, -0.699, -0.523, -0.398, -0.301, -0.222, -0.155, -0.097, -0.046, 0.0, 0.301, 0.477, 0.602, 0.699, 0.778, 0.845, 0.903, 0.954, 1.0, 1.301, 1.477, 1.602, 1.699, 1.778, 1.845, 1.903, 1.954, 2.0] Ticktext: ['0.01', '\n', '\n', '\n', '0.05', '\n', '\n', '\n', '\n', '0.1', '\n', '\n', '\n', '0.5', '\n', '\n', '\n', '\n', '1', '\n', '\n', '\n', '5', '\n', '\n', '\n', '\n', '10', '\n', '\n', '\n', '50', '\n', '\n', '\n', '\n', '100']