Export SVG out of matplotlib-venn - python

I have started to use matplotlib-venn for plotting venn diagram. It's a very useful tool, but I would like to know whether the graph generated can be saved in an SVG (or even pdf) format. I want to keep the graph vector, not rasterize it as in png.
I think there is a way, so if you can point me to it, that would be very helpful.

You can use the standard savefig method. Just give your output path a '.svg' extension:
from matplotlib_venn import venn2
import matplotlib.pyplot as plt
venn2(subsets = (3, 2, 1))
plt.savefig('venn2.svg')
You can save to PNG with a .png extension and... you can probably see where this is going for other formats.

Looks like you need to configure the SVG 'backend':
The matplotlib frontend or matplotlib API is the set of classes that
do the heavy lifting, creating and managing figures, text, lines,
plots and so on (Artist tutorial). This is an abstract interface that
knows nothing about output. The backends are device-dependent drawing
devices, aka renderers, that transform the frontend representation to
hardcopy or a display device (What is a backend?). Example backends:
PS creates PostScript® hardcopy, SVG creates Scalable Vector Graphics
hardcopy,...
> # The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
> # CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
> # Template.
> # You can also deploy your own backend outside of matplotlib by
> # referring to the module name (which must be in the PYTHONPATH) as
> # 'module://my_backend'. backend : qt4agg
Src: http://matplotlib.org/Matplotlib.pdf

Related

Is there a way to draw matplotlib plots to a window in some other 2D rendering library?

The idea would be that you'd have some 2D library for your GUI like PyGame and then draw the plots into the window generated by the library instead of an independent window generated by matplotlib. Allowing for user input and plot viewing in the same window.
I don't have any specific 2D library in mind, I'm not super familiar with Python libraries.
The only way I could find is by saving the plots to a file and then rendering that, but that seems extremely costly for real-time rendering (which is what I'm considering).
You can embed a plot from matplotlib into a tkinter app, check out their documentation here!
https://matplotlib.org/3.1.0/gallery/user_interfaces/embedding_in_tk_sgskip.html
Tkinter is a built-in package with cross platform support.

Use both matplotlib inline and qt in jupyter notebook

I am using Jupyter (with IPython) to analyze research data, as well as export figures. I really like the notebook approach offered by Jupyter: when I revisit an experiment after a long time, I can easily see how the figures correspond to the data. This is of course using the inline backend.
However, when I want to explore new data, I prefer to use the QT backend. It is faster than the inline one, and allows to easily scale, zoom in and out, and nicely displays the X and Y coordinates in the bottom left corner. Moreover, I can use the QT backend to determine good x and y limits to use in the inline backend.
I have tried using the %matplotlib notebook magic, but it is simply too slow. For some experiments I am plotting ~500 spectra (each consists of ~1000 data points), which is already slow in the inline backend. Even with less data points, the notebook backend is just too slow to use.
Therefore, I would like to use both the QT backend, and the inline backend whenever I plot something. (So, whenever I execute a cell which plots data, it should both display the inline image, and pop up a QT backend window). This way, I still have a nice overview of plots in my notebook, while also allowing me to easily explore my data. Is there a way to achieve this?
As far as I know it is not possible to use more than one backend at once.
What I do is to switch backend and replot. I have done it with matplotlib.use() or %matplotlib <backend>, according to which version or environment I was working in.
I had the impression that this sometimes doesn't work or maybe it is not responsive, so I need to restart the environment. I imagine it is not easy to maintain it, so I am even surprised it works so smoothly almost always.
Available backends can be listed with:
matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends
Hopefully here there are other useful info:
https://matplotlib.org/stable/tutorials/introductory/usage.html
This allows you to run QtConsole, plotting with the plotSin function, both inline and through the QtConsole.
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
..
def plotChirp(Type, Exp, Rand):
# Orignal Chirp Funciton From:
# http://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline
x = np.linspace(0, 3*np.pi, Rand)
plt.plot(x, np.sin(x**int(Exp)))
plt.title('A simple chirp ' + Type)
plt.show()
..
plotChirp("A", 5, 200) # Plots inline if you choose
%connect_info # For your own connection
%qtconsole
QtConsole opens and now you can call your function to plot externally..
Using %matplotlib qt allows for printing in a loop, but unfortunately it seems to overlap the plots. Looking into subplots as a possible solution.
%matplotlib qt
for i in range(0,2):
if i == 0:
plotChirp("B",1, 400)
else:
plotChirp("c",6, 1000)

Image Analysis in Bokeh?

I am looking to replace a MATLAB GUI used to do manual scoring of objects within a tif file. Someone recommended Bokeh to me. Is it possible to read a tif-file using any module and allow interactivity via Bokeh?
I have not used Bokeh (but I have kept an eye on it), so my answer is: Yes, you could probably use Bokeh for this.
But considering that you are replacing a MATLAB GUI, I think there might be an easier way. Bokeh creates plots that run in your web browser, which might not be what you want.
Instead, you could look into using matplotlib to do it.
The PyPlot API in matplotlib is very similar to the plot functions in MATLAB, and it supports creating interactive plots.
To read the TIFF file I would recommend looking into either PIL or OpenCV.

Is there a way to generate Retina graphs using 'matplotlib'?

Python's matplotlib does not generate Retina display quality LaTeX text by default. Are there settings or add-on package that generate Retina quality output?
Are you using 'Agg' mode (standard) to create PNG Graphs?
You can create SVG Graphs with Matplot lib which have way better quality using
matplotlib.use('svg')
If you look into texmanager.py you see that it makes the png by a call to dvipng and the function that makes the system call to dvipng has an argument for dpi
To get higher resolution TeX on a per-figure basis for existing figures
plt.gcf().canvas.renderer.dpi = what_ever_retina_dpi_is
to set it everywhere, you should be able to set the rcparam figure.dpi which will be respected when creating new renderers.
I can't test this as I do not have access to a Mac with a Retina display.

Tool to create a python GUI for graph construction

I need to create a GUI for graph construction ("graph" as an abstract representation of a set of objects, not a visual representation of data). The interface will provide a choice of ~5 vertex types and of ~5 edge types. Each vertex will have two data fields: a text label and a file name, which need to be easily editable.
I'm familiar with igraph and have a lot of code written in it. I will use igraph to manipulate the graphs created with this GUI.
Since this will be my first GUI, I'm completely ignorant of what tools are available. Can you please suggest a free library, knowing that eventually the program will need to work on Windows?
EDIT
it seems from the answers I get that I wasn't clear enough. I'm not looking for a way to visualize a graph, but rather for a way to visually create one. By visually, I mean not needing to manually create text files or writing code.
Take a look at xdot.py.
From the homepage
xdot.py is an interactive viewer for graphs written in Graphviz's
dot language.
It uses internally the graphviz's xdot output format as an
intermediate format, and PyGTK and Cairo for rendering.
xdot.py can be used either as a standalone application from
command line, or as a library embedded in your python application.
I like networkx,
from networkx import draw, Graph
from pylab import show
g = Graph()
g.add_edges_from([(1,2),(1,3),(2,4),(2,5)])
draw(g)
show()
which gives,
The only quirk is the requirement for matplotlib to get builtin plotting to work.
If you use python, I think PyQt is a good selection.
What you have to install is listed below:
install Python from here
install PyQt4 from here
But it takes many lines to write GUI application,
it is sometimes better to generate an image to display with image viewer.

Categories

Resources