Unable to use subplots with mplfig

Hello all,

I am trying to use mplfig parameter of the graph_draw to draw 4 graphs on
the same plot. My code is given below. However, this only produces graphs
in first and second subplots. What am I missing?

import graph_tool.all as gt
import matplotlib.pyplot as plt
plt.switch_backend('cairo')

'''Load a graph'''
g = gt.collection.data['karate']

for ind in range(4):

    ax = plt.subplot(2, 2, ind+1)
    gt.graph_draw(g, mplfig = ax)

plt.savefig('karate4states.pdf')

Thank you

attachment.html (1.08 KB)

Can somebody kindly help me with this? I am completely stuck.

Thank you

attachment.html (1.85 KB)

Funny thing, messing around with your example I find that graph_draw seems
to be drawing onto the wrong axis: when you pass axis 3 it draws inside
aixs 1, passing axis 4 draws inside axis 2, and what you pass as axes 1 and
2 is likely being drawn outside the canvas. Also, if you exapnd to 8 axes,
it plots only 4 of them, so it seems things are being shifted halfway
updards before drawing. I looked at the code but having never played with
Cairo it is not evident what could be wrong, but hopefully this will help.

.~´

attachment.html (3.26 KB)

Thanks for the reply. I don't think that there is any issue with graph
tool. It simply looks like ignorance on my part because I have seen some
figures in Tiago's papers which contain many network papers.

attachment.html (4.53 KB)

Dear Tiago,

Can you kindly suggest some solution on this?

Thank you

attachment.html (5.44 KB)

There seems to be a problem with the code, as Alexandre pointed out. I need
to take a closer look, but I'm out of time.

Please refrain from posting repeated messages like this. If I haven't
answered yet, is because I did not have a chance. Posting more messages does
not help.

Best,
Tiago

Hi Tiago,

I am on Ubuntu 16.04 and today I updated from git: '2.23 (commit b669e1b7,
Sun Sep 24 00:01:08 2017 +0200)'.

Till now, I was at least able to draw two graphs in two subplots as said in
my previous email. However, today even that is not working. My code:

import graph_tool.all as gt
import matplotlib.pyplot as plt
plt.switch_backend('cairo')

g1 = gt.collection.data['karate']
g2 = gt.collection.data['dolphins']

ax = plt.subplot(1, 2, 1)
gt.graph_draw(g1, mplfig = ax)
ax = plt.subplot(1, 2, 2)
gt.graph_draw(g2, mplfig = ax)
ax = plt.subplot(2, 2, 3)

plt.savefig('test.pdf')

I get the following error:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    plt.savefig('abc.pdf')
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 688, in
savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1565, in
savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line
2232, in print_figure
    **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_cairo.py",
line 448, in print_pdf
    return self._save(fobj, 'pdf', *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/backend_cairo.py",
line 512, in _save
    self.figure.draw (renderer)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in
draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1159, in
draw
    func(*args)
  File "/usr/lib/python3/dist-packages/matplotlib/artist.py", line 61, in
draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_base.py", line
2324, in draw
    a.draw(renderer)
  File "/usr/lib/python3/dist-packages/graph_tool/draw/cairo_draw.py", line
1570, in draw
    self.vorder, self.eorder, self.nodesfirst, self.kwargs)
  File "/usr/lib/python3/dist-packages/graph_tool/draw/cairo_draw.py", line
692, in cairo_draw
    vdefs = _attrs(_vdefaults, "v", g, vcmap)[1]
  File "/usr/lib/python3/dist-packages/graph_tool/draw/cairo_draw.py", line
486, in _attrs
    defaults[int(attr)] = _convert(attr, v, cmap)
  File "/usr/lib/python3/dist-packages/graph_tool/draw/cairo_draw.py", line
323, in _convert
    cmap, alpha = cmap
ValueError: not enough values to unpack (expected 2, got 0)

I tried looking into "cairo_darw.py" but couldn't locate the problem. I
think that it might have something to do with the default color map
default_cm but I am not sure about it. Can you kindly help me with this?

Thank you

attachment.html (5.03 KB)

This is a bug. Please open an issue at the website and I will deal with it.

Best,
Tiago

Dear Tiago,

Is there any workaround to this mplfig bug before it gets fixed? I am
manually trying to merge the output files of individual graphs in gimp but
that reduces the quality of the plots too much.

Thank you
Snehal

attachment.html (1.68 KB)

Hi Snehal. Hm. There is no reason why you should lose resolution while
merging files, with gimp or any other tool. Perhaps you should be setting
your new document to 4 times the resolution of the original, if you're
merging 4 files. Otherwise if you're working with SVGs you might prefer
Inkscape, which works directly on vector graphics. Saudações, ale

attachment.html (2.39 KB)

Thanks Alexandre for the reply. In some sense, it is true that gimp doesn't
deter the quality if I adjust the resolutions properly. My problem is that
when I include those plots in latex, the resulting plots in the output pdf
look really bad. I tried using higher edge_pen_width as well as putting a
black hollow around the vertices but doesn't improve that much.

Snehal

attachment.html (3.46 KB)

There are many options available, e.g. you can join using inkscape, or much
more simply, you can just include several files in the same figure in LaTeX.

Using gimp is often a bad idea since it rasterizes the figure into a bitmap,
where often it's best to preserve the image as a PDF, for example.

In any case, you can find this sort of information in many other places, and
is not really a good subject for this mailing list: http://bfy.tw/EU3S

Thank you, Tiago for the suggestions.

attachment.html (2.04 KB)