1
votes

I have generated these histograms with the python code below, and it looks fine in maptlotlib:

d_norm_1 = np.random.normal(loc=0.0, scale=3.0, size=5000)

## Build a Gaussian Mixture Model:
array1 = np.random.normal(loc=4.0, scale=2.0, size=2000)
array2 = np.random.normal(loc=-5.0, scale=4.0, size=2000)
d_norm_2 = np.concatenate((array1, array2))

fig3 = plt.figure(3, figsize=(8, 6))
ax3 = fig3.add_subplot(1, 1, 1)

plt.hist(d_norm_1, bins=40, normed=True, color='b', alpha=0.4, rwidth=1.0)
plt.hist(d_norm_2, bins=40, normed=True, color='g', alpha=0.4, rwidth=0.8)

plt.xlabel('$x$', size=20)
plt.ylabel('Probability Density', size=20)
plt.title('Histogram', size=20)

plt.setp(ax3.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax3.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()

enter image description here

But when I import this into plotly, the histogram bars are replaced by lines. I think plotly is not compatible with this version of matplotlib.

Here is the plotly version of the same histogram shown above:

https://plot.ly/~vmirjalily/11/histogram/

I am using matplotlib 1.4.2

2
I'm pretty sure plotly correctly recognizes this as a bar graph- I was able to go into the 'Traces' dialog and reduce the bar gap to make the bars show up a bit better. I think the problem is mostly to do with plotly's import API though, which probably makes it more of a question you should be sending to plotly support than something suited for SO. - Marius

2 Answers

3
votes

Your code histogram to plotly is working.

You are just missing one last step. What your plotly shows is a grouped bar chart. Eseentially what plotly has done is display 2 bars in a single column.

What you need to do, is go to

traces > mode and change to 'overlay' bar chart

here's my implementation

https://plot.ly/1/~quekxc

0
votes

biobirdman's solution is perfectly fine if you want to use the web tools. Here's another way to do it strictly from Python:

import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py

d_norm_1 = np.random.normal(loc=0.0, scale=3.0, size=5000)

## Build a Gaussian Mixture Model:
array1 = np.random.normal(loc=4.0, scale=2.0, size=2000)
array2 = np.random.normal(loc=-5.0, scale=4.0, size=2000)
d_norm_2 = np.concatenate((array1, array2))

fig3 = plt.figure(3, figsize=(8, 6))
ax3 = fig3.add_subplot(1, 1, 1)

plt.hist(d_norm_1, bins=40, normed=True, color='b', alpha=0.4, rwidth=1.0)
plt.hist(d_norm_2, bins=40, normed=True, color='g', alpha=0.4, rwidth=0.8)

plt.xlabel('$x$', size=20)
plt.ylabel('Probability Density', size=20)
plt.title('Histogram', size=20)

plt.setp(ax3.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax3.get_yticklabels(), rotation='horizontal', fontsize=16)

# note the `update` argument, it's formatted as a plotly Figure object
# this says: "convert the figure as best you can, then apply the update on the result"
py.iplot_mpl(fig3, update={'layout': {'barmode': 'overlay'}})

For more online info, checkout https://plot.ly/matplotlib/ or https://plot.ly/python/

For python help, checkout help(py.iplot_mpl) or help(Figure)

It can sometimes be useful to see exactly what got converted as well, you might try this:

import plotly.tools as tls
pfig = tls.mpl_to_plotly(fig3)  # turns the mpl object into a plotly Figure object
print pfig.to_string()  # prints out a `pretty` looking text representation