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

Plotly can't display negative bars from matlotlib figures

I ran into a an issue with tls.mpl_to_plotly(matplotlib_figure) The matplotlib figure has negative value, and it plots fine.

However, when I convert it to using tls.mpl_to_plotly, the negative bars will become positive, and I really don’t know how to solve it.

I have tried to set [‘layout’][‘barmode’] =‘relative’, which didn’t help. Could any provide any suggestions on how to fix this?

Your help is greatly appreciated!

try setting plotly_fig['data'][i]['base'] = 'relative' where i is the index of each bar trace in your graph.

another problem I had was that tls.mpl_to_plotly converted the negative numbers in the y axis to positive ones and I had to change them back manually. See my example code below. Even after setting base mode to relative the chart didn’t work and I needed to reset the y axis.

I’m sorry this is such a work-around.

import matplotlib.pyplot as plt

import plotly.offline as py
# Learn about API authentication here:
# Find your api_key here:

y = [3, 10, -7, 5, 3, 4.5, 6, 8.1]
N = len(y)
x = range(N)
width = 1/1.5, y, width, color="blue")

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly(fig)

plotly_fig['data'][0]['base'] ='relative'