plotly / plotly/plotly.py

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

Offen
#5,059 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug P3
Vorherrschende Sprache
Python
Sterne
18.8k
Forks
2.8k
Ø Merge
16 Std. 26 Min.
Gemergte PRs (30 T.)
21

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in plotly/matplotlylib/mpltools.py bei prep_ticks und reproduziere anschließend das bereitgestellte matplotlib-Balkendiagramm und die Konvertierung mit mpl_to_plotly. Behalte die ursprünglichen Achsenbeschriftungen in der konvertierten Plotly-Figur bei und überprüfe, dass das resultierende Diagramm der demonstrierten beschrifteten Ausgabe entspricht, ohne die Behandlung von Datumsachsen zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.