1

I am trying to plot the Matplotlib data on my browser using mpld3 library. But the problem that I am getting is I am getting the following Error :

if fig.canvas is None: AttributeError: 'list' object has no attribute 'canvas'

These are the mpld3 objects that I imported.

import matplotlib.pyplot as plt,mpld3
from mpld3 import save_json, fig_to_html, plugins

Here is my code to save the figure in HTML .

plt.subplots(1, 1, figsize=(8, 2))
ecg = X
fig=plt.figure()
alt = np.arange(len(ecg))/125
fig= plt.plot(alt,ecg)
mpld3.save_html(fig,"test.html")
mpld3.fig_to_html(fig,template_type="simple")
mpld3.disable_notebook()
mpld3.show()

I am getting the figure if I am not saving the data in html file. The html file is created but is empty and the Above Mentioned error is being showed on the console.

Please Help. Any Help is really appreciated

Here is the figure what it looks like. enter image description here

Chempooro
  • 415
  • 1
  • 7
  • 24

1 Answers1

0

From following the documentation here, I was able to get the plot to be sent to the html file as follows:

import matplotlib.pyplot as plt,mpld3
from mpld3 import save_json, fig_to_html, plugins, save_html
import numpy as np

X = 'x'

fig, ax = plt.subplots(1, 1, figsize=(8, 2))
ecg = X
fig = plt.figure()
alt = np.arange(len(ecg))/125
lines = ax.plot(alt,ecg)
mpld3.plugins.connect(fig, mpld3.plugins.LineLabelTooltip(lines[0]))
mpld3.fig_to_html(fig)
mpld3.save_html(fig,"test.html")

# mpld3.disable_notebook()
mpld3.show()

Your line of

fig=plt.figure()

fig= plt.plot(alt,ecg)

Does not make use of the figure method, which is why you are getting the 'canvas' error.

I am not sure what all your data is, and you probably do not need to use the plugin method, but switching the order of saving the html, and converting the fig to html, seems to have resolved some issues.

Hope this helps.

Sky020
  • 47
  • 1
  • 7
  • Ok now. I am not getting any error . but the html page that opens is blank now – Chempooro Aug 09 '19 at 10:52
  • If I can store the data in json file. that will solve my problems . but the issue is that json file gets created. but is empty – Chempooro Aug 09 '19 at 10:53
  • @AbnitChauhan, The created HTML file is not complete. I think i will need necessary elements added. – Sky020 Aug 09 '19 at 15:51