I know Matplotlib can be used in Django, based on the documentation. However, I'd like to be able to create interactive graphs within a webpage (i.e clickable points). In order to display a graph created in a Python script, Django spits out a png image. I haven't tested it out myself, but the fact that Django only spits out a png is worrisome. Is what I'm trying to do possible?
http://scipy-cookbook.readthedocs.io/items/Matplotlib_Django.html
http://scipy-cookbook.readthedocs.io/items/Matplotlib_Interactive_Plotting.html
Related
I have an interactive graph generated by Plotly in Python that I saved to an html file using plotly.offline.plot(fig, filename='/tmp/interactiveGraph.html')
I am now trying to embed this interactive graph into some kind of webpage, using either Dash or Django. I'm leaning toward Django at the moment, given that I have an html version of the graph. Which would be better?
My code for the webpage is in a separate file from the code where I am creating the graph.
A lot of the tutorials I've found online just say to add a few lines to the template, but I don't know how to get those lines that they've described.
tl;dr: I'm looking for guidance as how to integrate an html file-for a Plotly interactive graph-with a web python script using Django or Dash
Side Question:
what is the difference between
plotly.offline.plot(fig, include_plotlyjs=False, output_type='div')
and what I have above?
Reference:
https://github.com/ricleal/DjangoPlotLy
https://www.pythonsetup.com/how-implement-data-visualization-django-and-plotly/
I would highly reccomend Django, its a great framework. As for this, the best option is to generate the data via JavaScript and Plotly has a great library for this. If you must use python, then Django can be used. Assuming you are familiar with Django, inside of your view you can collect your data and build your graph ( I would reccomend a celery task for something long running like this unless they are small graphs), and you can then collect the return from creating the graph in div format. This will return a string that holds all the needed html and css for the graphs. Put this inside of you get_context_data() method in your view and place it into the dictionary. You can the use that object inside of a template. I have done this before, if you are having a hard time feel free to DM me. Hope this helps some!
In regards to your side question, I believe having False for including JS will make the graph a bit smaller assuming you have an include for the plotly JS library. They might have done this in a newer release to make the graphs faster as they were significantly slower in the browser from python that the JS rendered one.
So, I'm learning how to use matplotlib and stumbled into MPLD3 as the most used way to get these plots in a webpage. However, MPLD3 doesn't support tick formatting and that's something critical for the project I'm in right now. I'd like to know if there is another way to add a matplotlib graph to a webpage while keeping the tick formatting and also having tooltips to display data on hover.
Thanks!
For a web application I would consider bokeh. You can output html with interactive charts or run another backend server. If you go to the ipython notebook tutorial section 10 has demos for embedding.
Alternatively you can just save images from matplotlib and use them as static assets in your page.
I'd like to output the image to the web created in matplotlib having the very same functionality like it has on desktop when you run the image.show(), for example scaling, moving along the plot more thoroughly.
I've checked out #stack and got old post only offering static images or gif or matplotlib.animate()
I aslo had a look at matplotlib widgets, but those are for desktop GUI only as far as I can see.
Please share some experience or ideas regarding how can I achieve it.
Thanks
Matplotlib is a server side library so you cannot do anything on the client side like that.
The closest you can come is to either use mpld3 (mpld3 works by converting a matplotlib graph into the html/js that a d3 js graph would need to render) or a different client side library that plots points.
The easiest way of serving pure matplotlib to the web is using a jupyter notebook.
Other than that, you may want to look at specific libraries like Plotly or bokeh.
I am working on generating some plots using python, but I am generating these plots using matplotlib which is saved as images. If I create an html page as a report with these plots, they are static images. I cannot zoom in or roll over on the plot to see more detailed or specific information on a time series plot.
My question is how can I make these plots dynamic? Can someone suggest the best way to get started and move forward from there?
You should use some additional libraries to achive your goal.
For example, there some good Python web frameworks wich you can use:
CherryPy - allows you to simply write web-app with Python and you can import your plot there.
Plotly Python API - it would simply generate interactive plot, but store it at Plotly platform, but they provide embeded-code option, so you can use it on your site.
I would suggest Plotly, because it is much simpler, but it depends on your needs.
You will definitely want to do it using javascript. It's by far your best option when it comes to quickly make interactive graphs that you can present to a lot of users. Any of these js libraries will do a great job.
You will then want python to provide the data. Depending on the js library you are using, you might be able to parse data from .json, .csv, etc...
If you don't need the data that makes up your plots to change (with user input, for example), then generating and saving flat files with python and having javascript parsing them from some directory might be just enough.
Otherwise, you want to take a look at a python web framework and use one as backend to serve the plots data by request (in that case .json is probably the right format).
Frameworks like Flask, CherryPy, Pyramid or even web2py might be the easier ones to start with.
Are there any packages that allow for the interactive display of GIS shapefiles? I'm looking to create a simple GUI that displays simple shapefiles (coastlines, etc) but can't seem to find where to start. I was originally tasked with doing this in R but I would like to try and use python and Qt.
Mapnik supports GIS shape files and has a Python interface.
I think you can use a combination of the shapefile library for reading files, and shapely for all the processing of the data.
I have used shapely for working with geo data feeds that have been loaded into a database, to work with the point and polygon data.
Per your comments, if you just want to simply display a shapefile in your PyQt app, then really all you need is to convert it to an SVG and display it directly. I have not used this conversion lib but I am sure there are more like it. Then you can just use a QSvgWidget to load and display it