Skip to content

Commit 7701120

Browse files
Add support for overriding x tick with non arithmetic progression values
1 parent c0740bf commit 7701120

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

plotly/matplotlylib/mpltools.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ def prep_ticks(ax, index, ax_type, props):
436436
tick0 = tickvalues[0]
437437
dticks = [
438438
round(tickvalues[i] - tickvalues[i - 1], 12)
439-
for i in range(1, len(tickvalues) - 1)
439+
for i in range(1, len(tickvalues))
440440
]
441-
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks) - 1)]):
441+
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks))]):
442442
dtick = tickvalues[1] - tickvalues[0]
443443
else:
444444
warnings.warn(
@@ -448,6 +448,8 @@ def prep_ticks(ax, index, ax_type, props):
448448
raise TypeError
449449
except (IndexError, TypeError):
450450
axis_dict["nticks"] = props["axes"][index]["nticks"]
451+
if props["axes"][index]["tickvalues"] is not None:
452+
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
451453
else:
452454
axis_dict["tick0"] = tick0
453455
axis_dict["dtick"] = dtick
@@ -496,6 +498,15 @@ def prep_ticks(ax, index, ax_type, props):
496498

497499
if formatter == "LogFormatterMathtext":
498500
axis_dict["exponentformat"] = "e"
501+
elif (
502+
formatter == "FuncFormatter" and props["axes"][index]["tickformat"] is not None
503+
):
504+
to_remove = ["dticktickmode"]
505+
for key in to_remove:
506+
if key in axis_dict:
507+
axis_dict.pop(key)
508+
axis_dict["ticktext"] = props["axes"][index]["tickformat"]
509+
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
499510
return axis_dict
500511

501512

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,28 @@ 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+
90+
def test_non_arithmetic_progression_xtickvals():
91+
xticks = [0.01, 0.53, 0.75]
92+
plt.figure()
93+
plt.plot([0, 1], [0, 1])
94+
plt.xticks(xticks)
95+
96+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
97+
98+
assert plotly_fig.layout.xaxis.tickvals == tuple(xticks)
99+
100+
101+
def test_non_arithmetic_progression_xticktext():
102+
xtickvals = [0.01, 0.53, 0.75]
103+
xticktext = ["Baseline", "param = 1", "param = 2"]
104+
plt.figure()
105+
plt.plot([0, 1], [0, 1])
106+
plt.xticks(xtickvals, xticktext)
107+
108+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
109+
110+
assert plotly_fig.layout.xaxis.tickvals == tuple(xtickvals)
111+
assert plotly_fig.layout.xaxis.ticktext == tuple(xticktext)

0 commit comments

Comments
 (0)