Skip to content

Add support for overriding x tick with non arithmetic progression values #5262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions plotly/matplotlylib/mpltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,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 @@ -464,6 +464,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 @@ -512,6 +514,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
25 changes: 25 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import plotly.tools as tls

from . import plt

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)