2
votes

I am trying to plot a bar chart in offline mode in Jupyter Ipython notebook. I am trying to reproduce the bar chart in the second example of this page: https://plot.ly/matplotlib/bar-charts/

Here is my code:

import plotly
import datetime
import matplotlib.pyplot as plt
%matplotlib inline

#Here I changed my personal username and API_key with the one provided on 
#plotly's "getting started page", though I believe I should not need to sign in
#in offline mode
plotly.plotly.sign_in('DemoAccount', 'lr1c37zw81')

#Here is the matplotlib figure I would like to load in plotly
date = plt.figure()
x = [datetime.datetime(2010, 12, 1, 10, 0),
datetime.datetime(2011, 1, 4, 9, 0),
datetime.datetime(2011, 5, 5, 9, 0)]
y = [4, 9, 2]
ax = plt.subplot(111)
ax.bar(x, y, width=10)
ax.xaxis_date()
plt.show()

plotly.offline.init_notebook_mode()

plotly.plotly.plot_mpl(date)

However, this code fails to load the picture inside the Ipython notebook (the chart is loaded in a new tab).

Can you help me out?

PS: I am using Python 3.4.3 and Plotly 1.9.0 .

1

1 Answers

1
votes

Plotly version 1.9.5 now offers an offline option for matplotlib to plotly figure conversion (per https://github.com/plotly/plotly.py/blob/e3b0167ba6639cc5977b18e1e611339ee5ced809/CHANGELOG.md). The following works for me with Python 3.4.4 and 2.7.10 and Plotly 1.9.6:

import plotly
print(plotly.__version__)
import datetime
import matplotlib.pyplot as plt
import plotly.plotly as py
from plotly.offline import init_notebook_mode, enable_mpl_offline, iplot_mpl
init_notebook_mode()
enable_mpl_offline()

date = plt.figure()
x = [datetime.datetime(2010, 12, 1, 10, 0),
datetime.datetime(2011, 1, 4, 9, 0),
datetime.datetime(2011, 5, 5, 9, 0)]
y = [4, 9, 2]
ax = plt.subplot(111)
ax.bar(x, y, width=10)
ax.xaxis_date()

iplot_mpl(date)