`mpl_to_plotly` does not preserve axis labels (bar plots are useless)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 18.8k
- Forks
- 2.8k
- Avg merge
- 16h 26m
- Merged PRs (30d)
- 21
Description
While mpl_to_plotly is little known and receives little love, this bug is pretty easy to fix. Would you be open to a PR?
import matplotlib.pyplot as plt
from plotly.tools import mpl_to_plotly
from plotly.offline import plot
In matplotlib this produces a barplot with labels:
labels = ['a', 'b', 'c']
values = [1, 2, 3]
f = plt.figure(figsize=(6, 4))
plt.bar(labels, values)
plt.tight_layout()
But conversion to plotly looses the labels:
plotly_fig = mpl_to_plotly(f)
plot(plotly_fig)
A minimal fix would be to modify prep_ticks by appending:
if axis_dict.get("type") == "date":
return axis_dict
vals = []
texts = []
for tick in axis.majorTicks:
vals.append(tick.get_loc())
texts.append(tick.label1.get_text())
if texts:
axis_dict = {}
axis_dict['tickmode'] = 'array'
axis_dict['tickvals'] = vals
axis_dict['ticktext'] = texts
return axis_dict
which produces:
prep_ticks is defined in:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in plotly/matplotlylib/mpltools.py at prep_ticks, then reproduce the supplied matplotlib bar plot and mpl_to_plotly conversion. Preserve the original axis labels in the converted Plotly figure, and verify the resulting plot matches the demonstrated labeled output without regressing date-axis handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100