Python Pyx: remove repeated color bars in density plot

116 views Asked by At

I wanna have multiple density plots in one Python Pyx plot.

I can do this to have two density plots:

enter image description here

Question: how to remove unecessary repeated color bar?

Example code is from here:

 from pyx import *
 f = canvas.canvas()
 re_min = -2
 re_max = 0.5
 im_min = -1.25
 im_max = 1.25
 gridx = 100
 gridy = 100
 max_iter = 10

 re_step = (re_max - re_min) / gridx
 im_step = (im_max - im_min) / gridy
 d = []


 for re_index in range(gridx):
 re = re_min + re_step * (re_index + 0.5)
for im_index in range(gridy):
    im = im_min + im_step * (im_index + 0.5)
    c = complex(re, im)
    n = 0
    z = complex(0, 0)
    while n < max_iter and abs(z) < 2:
        z = (z * z) + c
        n += 1
    d.append([re, im, n])

  g1 = graph.graphxy(height=8, width=8,
              x=graph.axis.linear(min=re_min, max=re_max, title=r"$\Re(c)$"),
              y=graph.axis.linear(min=im_min, max=im_max, title=r'$\Im(c)$'))
              
    g1.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
   [graph.style.density(gradient=color.rgbgradient.Rainbow)])
 f.insert(g1)
   
 g2 = graph.graphxy(height=8, width=8, xpos=g1.xpos+14.0,
              x=graph.axis.linear(min=re_min, max=re_max, title=r"$\Re(c)$"),
              y=graph.axis.linear(min=im_min, max=im_max, title=r'$\Im(c)$'))
              
 g2.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"),
   [graph.style.density(gradient=color.rgbgradient.Rainbow)])   
  f.insert(g2)     

     f.writePDFfile()
1

There are 1 answers

0
wobsta On BEST ANSWER

The color bar is called a keygraph, and it is a property of the density style. You can set it to None, i.e.

graph.style.density(gradient=color.rgbgradient.Rainbow, keygraph=None)

which does not remove the (automatic) key graph internally, but it supresses its output.

You can also set the keygraph yourself, and in addition, you can set the coloraxis of this keygraph. It is a property of the style, too, and a simple linear axis by default, but can be changed (like fixing min and max values).

Now, when you suppress the keygraph, it is not sure, that the same axis will be used in both graphs (even if you share the same axis, as long as you remain using flexible ranges). There are various solutions out. Let me give a more advanced one. :-)

In the first graph, we could keep a copy of the plotitem, finish the plot (which creates the keygraph) and then access the coloraxis like this:

d1 = graph.style.density(gradient=color.rgbgradient.Rainbow)
plotitem = g1.plot(graph.data.points(d, x=1, y=2, color=3, title="iterations"), [d1])
f.insert(g1)
g1.finish()
coloraxis = plotitem.privatedatalist[-1].keygraph.axes['x']

Now you can use this coloraxis in the second graph while still supressing the keygraph:

d2 = graph.style.density(gradient=color.rgbgradient.Rainbow, keygraph=None,
                         coloraxis=graph.axis.linkedaxis(coloraxis))

This ensures the same scale in the keygraph and thus the same colors. :-)