Skip to content

Commit 2415c9b

Browse files
Add support for overriding x tick with non arithmetic progression values
1 parent 1ec864b commit 2415c9b

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

plotly/matplotlylib/mpltools.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,9 @@ def prep_ticks(ax, index, ax_type, props):
452452
tick0 = tickvalues[0]
453453
dticks = [
454454
round(tickvalues[i] - tickvalues[i - 1], 12)
455-
for i in range(1, len(tickvalues) - 1)
455+
for i in range(1, len(tickvalues))
456456
]
457-
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks) - 1)]):
457+
if all([dticks[i] == dticks[i - 1] for i in range(1, len(dticks))]):
458458
dtick = tickvalues[1] - tickvalues[0]
459459
else:
460460
warnings.warn(
@@ -464,6 +464,8 @@ def prep_ticks(ax, index, ax_type, props):
464464
raise TypeError
465465
except (IndexError, TypeError):
466466
axis_dict["nticks"] = props["axes"][index]["nticks"]
467+
if props["axes"][index]["tickvalues"] is not None:
468+
axis_dict["tickvals"] = props["axes"][index]["tickvalues"]
467469
else:
468470
axis_dict["tick0"] = tick0
469471
axis_dict["dtick"] = dtick
@@ -512,6 +514,13 @@ def prep_ticks(ax, index, ax_type, props):
512514

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

517526

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import plotly.tools as tls
2+
3+
from . import plt
4+
5+
def test_non_arithmetic_progression_xtickvals():
6+
xticks = [0.01, 0.53, 0.75]
7+
plt.figure()
8+
plt.plot([0, 1], [0, 1])
9+
plt.xticks(xticks)
10+
11+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
12+
13+
assert plotly_fig.layout.xaxis.tickvals == tuple(xticks)
14+
15+
def test_non_arithmetic_progression_xticktext():
16+
xtickvals = [0.01, 0.53, 0.75]
17+
xticktext = ["Baseline", "param = 1", "param = 2"]
18+
plt.figure()
19+
plt.plot([0, 1], [0, 1])
20+
plt.xticks(xtickvals, xticktext)
21+
22+
plotly_fig = tls.mpl_to_plotly(plt.gcf())
23+
24+
assert plotly_fig.layout.xaxis.tickvals == tuple(xtickvals)
25+
assert plotly_fig.layout.xaxis.ticktext == tuple(xticktext)

0 commit comments

Comments
 (0)