Black Lives Matter. Please consider donating to Black Girls Code today.

Matplotlib to plotly bar chart label loss

Hi all,
I decided I wanted to start using plotly, because I was very impressed by its features. I’ve already done a lot of work in matplotlib so I was hoping I could convert some of the graphs I’ve already made into plotly. However I’ve been running into a problem converting bar charts where if my xaxis consists of strings, then the bar labels get converted into numbers. Is there a way to keep the string labels that i’m missing??
Thanks in advance to for any help that can ben given!!

An example of this problem Is shown below

y = [3, 10, 7, 5]
x = [‘hi’, ‘hello’, ‘yo’, ‘gday’]

mpl_fig = plt.figure(figsize=(40,20))
ax = mpl_fig.add_subplot(111), y, color = ‘b’, alpha = 0.8)
plt.xticks(rotation=90, fontsize = 25)
plt.yticks(fontsize = 25)
plt.ylabel(‘x test’, fontsize = 32)
plt.xlabel(‘y test’, fontsize = 32)

plotly_fig = tls.mpl_to_plotly(mpl_fig)

producing the graph below

Hi @henry.k.turner,

Unfortunately the matplotlib conversion can only support a subset of the matplotlib (and plotly) plotting functionality. It does look like categorical axes labels get lost in the conversion at this point. If you’d like, you could create a issue at The matplotlib conversion support hasn’t gotten a lot of attention lately, but it would still be helpful to have this shortcoming logged.

I would be interested to understand your usecase a little more. Do you have matplotlib figures saved as files that you would like to convert? Or do you have a lot of existing scripts/notebooks with matplotlib plotting code that you’d like to append a conversion command to the end? Are you interested in converting the matplotlib plotting code to plotting code eventually?

I’m wondering if a more helpful workflow might be for us to take a matplotlib figure and produce the code that creates as much of the figure as possible. This way even if the resulting figure isn’t perfect, you’d hopefully have a good starting point for the conversion.

What do you think?

hi @jmmease

Thanks for you reply. I was interested in taking matplotlib plotting code and appending the conversion command to the end. I would be interested in converting the matplotlib code to code eventually. I can add a couple of examples of my matplotlib plotting code (though with anonymised variable names and labels) if this helps?