Skip to content

Commit d813535

Browse files
Add support for overriding x tick with non arithmetic progression values
1 parent 694b036 commit d813535

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
@@ -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

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)