Fix using logarithmic scale breaks matplotlib reporting

This commit is contained in:
allegroai 2022-08-29 18:00:59 +03:00
parent e30f497618
commit a6104347f2

View File

@ -417,43 +417,48 @@ class PatchedMatplotlib:
plotly_renderer = PatchedMatplotlib._matplotlylib.PlotlyRenderer() plotly_renderer = PatchedMatplotlib._matplotlylib.PlotlyRenderer()
PatchedMatplotlib._matplotlylib.Exporter(plotly_renderer, close_mpl=False).run(fig) PatchedMatplotlib._matplotlylib.Exporter(plotly_renderer, close_mpl=False).run(fig)
x_ticks = list(plotly_renderer.current_mpl_ax.get_xticklabels()) def process_tick_text(ticks, axes, val_index):
if x_ticks: if not ticks:
return
# noinspection PyBroadException # noinspection PyBroadException
try: try:
# check if all values can be cast to float # check if all values can be cast to float
_ = [float(t.get_text().replace('', '-')) for t in x_ticks] _ = [float(t.get_text().replace("", "-")) for t in ticks]
except Exception: except Exception:
# noinspection PyBroadException # noinspection PyBroadException
try: try:
_xaxis = next(x for x in ('xaxis', 'xaxis0', 'xaxis1') def convert_latex_math_powers_to_html(text):
if x in plotly_renderer.plotly_fig['layout']) # noinspection PyBroadException
plotly_renderer.plotly_fig['layout'][_xaxis].update({ try:
'ticktext': [t.get_text() for t in x_ticks], if not text or not text.startswith("$\\mathdefault{") or not text.endswith("}}$"):
'tickvals': [t.get_position()[0] for t in x_ticks], return text
}) numbers = text[len("$\\mathdefault{"): -len("}}$")].split("^{")
plotly_renderer.plotly_fig['layout'][_xaxis].pop('type', None) if len(numbers) != 2:
except Exception: return text
pass base, exp_ = numbers[0], numbers[1]
y_ticks = list(plotly_renderer.current_mpl_ax.get_yticklabels()) return "{}<sup>{}</sup>".format(base, exp_)
if y_ticks: except Exception:
# noinspection PyBroadException return text
try:
# check if all values can be cast to float _axis = next(x for x in axes if x in plotly_renderer.plotly_fig["layout"])
_ = [float(t.get_text().replace('', '-')) for t in y_ticks] plotly_renderer.plotly_fig["layout"][_axis].update(
except Exception: {
# noinspection PyBroadException "ticktext": [convert_latex_math_powers_to_html(t.get_text()) for t in ticks],
try: "tickvals": [t.get_position()[val_index] for t in ticks],
_yaxis = next(x for x in ('yaxis', 'yaxis0', 'yaxis1') }
if x in plotly_renderer.plotly_fig['layout']) )
plotly_renderer.plotly_fig['layout']['_yaxis'].update({ if plotly_renderer.plotly_fig["layout"][_axis].get("type") == "linear":
'ticktext': [t.get_text() for t in y_ticks], plotly_renderer.plotly_fig["layout"][_axis].pop("type", None)
'tickvals': [t.get_position()[1] for t in y_ticks],
})
plotly_renderer.plotly_fig['layout'][_yaxis].pop('type', None)
except Exception: except Exception:
pass pass
process_tick_text(
list(plotly_renderer.current_mpl_ax.get_xticklabels()), ("xaxis", "xaxis0", "xaxis1"), 0
)
process_tick_text(
list(plotly_renderer.current_mpl_ax.get_yticklabels()), ("yaxis", "yaxis0", "yaxis1"), 1
)
# try to bring back legend # try to bring back legend
# noinspection PyBroadException # noinspection PyBroadException
try: try: