0

I am trying to plot a stacked graph using Matplotlib in HTML page (python -cgi). I have the following script

`#!/usr/bin/python 
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt, mpld3
data = {'title1': {'SA':20, 'TA':10}, 'title2':{'FA':10, 'MA':20, 
'NA':30}}
grap=pd.DataFrame(data).T.plot(kind='bar', stacked=True,figsize= . 
(16,18))
mpld3.fig_to_html(grap)
print '</body>'
print '</html>'`

However, when i try to open the script in web browser, it shows error. If i remove "import mpdl3", the script works but the image is not seen.

Could any one help how to embed the plot into a html page.

Thanks in advance.

ford prefect
  • 7,096
  • 11
  • 56
  • 83
Sam
  • 145
  • 11
  • If this is for regular usage and something you will be doing a lot of, you should look into setting up and learning Django. We use it all the time to render matplotlib plots to a web page. – GeorgeLPerkins May 25 '18 at 14:55

1 Answers1

0

One alternative to this would be to the save the matplotlib plot as an image using something like plt.savefig("/file/path/something.jpg") and then referencing that like so <img src="/file/path/something.jpg" /> because the plot itself is rendering to a figure not to stdout which is where print statements go.

Basically you are misunderstanding what the difference between stdout and figure is. Figure is a python utility that opens a window and shows an image (in this case a pyplot graph) while standard out is the text output of your python code.

mpld3 Specific

So I looked into mpld3 more. The saving and using img won't actually allow for actual integration on the order of what you are looking for. It will allow you to show a graph in a web page but it will strip away the interactivity. My guess is that the real problem here is that apache is looking at a different python install. Check and see if mpld3 is installed at the user or system level and check to see which python install apache is using.

Third Thought

It looks like you're importing mlpd3 incorrectly. According to this page it looks like you just import mlpd3 and not from matplotlib.

ford prefect
  • 7,096
  • 11
  • 56
  • 83
  • Thanks alot.. it worked.. Is there a way to use mpld3.. when i use in and run on apache server it shows error.. – Sam May 25 '18 at 13:11
  • can i know how to label the keys inside the bar.. for eg. SA has to be printed within the bar – Sam May 25 '18 at 14:26
  • @Sam that's a separate question and if you look around there are docs and questions on how to label the graph. My guess for mpld3 is that you don't have it installed in the same python location/session that you're using for your terminal as the apache server is using. That's just a guess though... What exactly does it print out when the import fails? – ford prefect May 25 '18 at 17:04
  • Hi Ford. I have corrected mlpd3 but still i get the following error:Server error! The server encountered an internal error and was unable to complete your request. Error message: End of script output before headers: Interaction.py If you think this is a server error, please contact the webmaster. Error 500 – Sam May 28 '18 at 07:33
  • however, when i searched in web it is mentioned as mpld3. Can you let me know how to check if it is installed at user level. i searched web but could not find any information on how to do it. – Sam May 28 '18 at 07:37
  • My guess is some sort of issue in your apache config or the way you're rendering the page. I doubt your issue is in how you're using the python library – ford prefect May 29 '18 at 12:28
  • Yes. there is some issue in my apache config. i did try many option like installing the package the usr/bin and so. but still the error exists – Sam May 31 '18 at 07:49
  • I'm gonna be inclined to go with a python specific webserver a la django or something like that. Also did you change your import to just `import mlpd3`? – ford prefect May 31 '18 at 12:40