Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for overriding x tick with non arithmetic progression values
  • Loading branch information
robertoffmoura committed Oct 9, 2025
commit 674d40eec54f37357517ebe6f0567fea5d116fdd
13 changes: 11 additions & 2 deletions plotly/matplotlylib/mpltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ def prep_ticks(ax, index, ax_type, props):
tick0 = tickvalues[0]
dticks = [
round(tickvalues[i] - tickvalues[i - 1], 12)
for i in range(1, len(tickvalues) - 1)
for i in range(1, len(tickvalues))
]
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks) - 1)]):
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks))]):
dtick = tickvalues[1] - tickvalues[0]
else:
warnings.warn(
Expand All @@ -463,6 +463,8 @@ def prep_ticks(ax, index, ax_type, props):
raise TypeError
except (IndexError, TypeError):
axis_dict["nticks"] = props["axes"][index]["nticks"]
if props["axes"][index]["tickvalues"] is not None:
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
else:
axis_dict["tick0"] = tick0
axis_dict["dtick"] = dtick
Expand Down Expand Up @@ -511,6 +513,13 @@ def prep_ticks(ax, index, ax_type, props):

if formatter == "LogFormatterMathtext":
axis_dict["exponentformat"] = "e"
elif formatter == "FuncFormatter" and props["axes"][index]["tickformat"] is not None:
to_remove = ["dtick" "tickmode"]
for key in to_remove:
if key in axis_dict:
axis_dict.pop(key)
axis_dict["ticktext"] = props["axes"][index]["tickformat"]
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
return axis_dict


Expand Down
23 changes: 23 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,26 @@ def test_multiple_traces_native_legend():
assert plotly_fig.data[0].mode == "lines"
assert plotly_fig.data[1].mode == "markers"
assert plotly_fig.data[2].mode == "lines+markers"


def test_non_arithmetic_progression_xtickvals():
xticks = [0.01, 0.53, 0.75]
plt.figure()
plt.plot([0, 1], [0, 1])
plt.xticks(xticks)

plotly_fig = tls.mpl_to_plotly(plt.gcf())

assert plotly_fig.layout.xaxis.tickvals == tuple(xticks)

def test_non_arithmetic_progression_xticktext():
xtickvals = [0.01, 0.53, 0.75]
xticktext = ["Baseline", "param = 1", "param = 2"]
plt.figure()
plt.plot([0, 1], [0, 1])
plt.xticks(xtickvals, xticktext)

plotly_fig = tls.mpl_to_plotly(plt.gcf())

assert plotly_fig.layout.xaxis.tickvals == tuple(xtickvals)
assert plotly_fig.layout.xaxis.ticktext == tuple(xticktext)