plotly / plotly/plotly.py

`mpl_to_plotly` does not preserve axis labels (bar plots are useless)

Open
#5,059 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
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()

Image

But conversion to plotly looses the labels:

plotly_fig = mpl_to_plotly(f)
plot(plotly_fig)

Image

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:

Image

prep_ticks is defined in:

https://github.com/plotly/plotly.py/blob/c54a2bdf1655caaaa3b7b71fbfc38a5584767bd5/plotly/matplotlylib/mpltools.py#L428-L514

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.