Skip to content

Commit 674d40e

Browse files
Add support for overriding x tick with non arithmetic progression values
1 parent bf29422 commit 674d40e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

plotly/matplotlylib/mpltools.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,9 @@ def prep_ticks(ax, index, ax_type, props):
451451
tick0 = tickvalues[0]
452452
dticks = [
453453
round(tickvalues[i] - tickvalues[i - 1], 12)
454-
for i in range(1, len(tickvalues) - 1)
454+
for i in range(1, len(tickvalues))
455455
]
456-
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks) - 1)]):
456+
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks))]):
457457
dtick = tickvalues[1] - tickvalues[0]
458458
else:
459459
warnings.warn(
@@ -463,6 +463,8 @@ def prep_ticks(ax, index, ax_type, props):
463463
raise TypeError
464464
except (IndexError, TypeError):
465465
axis_dict["nticks"] = props["axes"][index]["nticks"]
466+
if props["axes"][index]["tickvalues"] is not None:
467+
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
466468
else:
467469
axis_dict["tick0"] = tick0
468470
axis_dict["dtick"] = dtick
@@ -511,6 +513,13 @@ def prep_ticks(ax, index, ax_type, props):
511513

512514
if formatter == "LogFormatterMathtext":
513515
axis_dict["exponentformat"] = "e"
516+
elif formatter == "FuncFormatter" and props["axes"][index]["tickformat"] is not None:
517+
to_remove = ["dtick" "tickmode"]
518+
for key in to_remove:
519+
if key in axis_dict:
520+
axis_dict.pop(key)
521+
axis_dict["ticktext"] = props["axes"][index]["tickformat"]
522+
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
514523
return axis_dict
515524

516525

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,26 @@ def test_multiple_traces_native_legend():
8484
assert plotly_fig.data[0].mode == "lines"
8585
assert plotly_fig.data[1].mode == "markers"
8686
assert plotly_fig.data[2].mode == "lines+markers"
87+
88+
89+
def test_non_arithmetic_progression_xtickvals():
90+
xticks = [0.01, 0.53, 0.75]
91+
plt.figure()
92+
plt.plot([0, 1], [0, 1])
93+
plt.xticks(xticks)
94+
95+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
96+
97+
assert plotly_fig.layout.xaxis.tickvals == tuple(xticks)
98+
99+
def test_non_arithmetic_progression_xticktext():
100+
xtickvals = [0.01, 0.53, 0.75]
101+
xticktext = ["Baseline", "param = 1", "param = 2"]
102+
plt.figure()
103+
plt.plot([0, 1], [0, 1])
104+
plt.xticks(xtickvals, xticktext)
105+
106+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
107+
108+
assert plotly_fig.layout.xaxis.tickvals == tuple(xtickvals)
109+
assert plotly_fig.layout.xaxis.ticktext == tuple(xticktext)

0 commit comments

Comments
 (0)