1

Suppose I wanted to automatically select the size for square symbols in a scatter plot to make the borders align (a question like that has been asked).

In my answer to that question, I suggested that the distance between two data points measured in pixel could be used to set the size of the symbols in a scatter plot.

This is my approach (it was inspired by this answer):

fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

# initialize a plot to determine the distance between the data points in pixel:    
x = [1, 2, 3, 4, 2, 3, 3]
y = [0, 0, 0, 0, 1, 1, 2]
s = 0.0
points = ax.scatter(x,y,s=s,marker='s')
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.])

# retrieve the pixel information:
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T

# In matplotlib, 0,0 is the lower left corner, whereas it's usually the upper 
# right for most image software, so we'll flip the y-coords
width, height = fig.canvas.get_width_height()
ypix = height - ypix

# this assumes that your data-points are equally spaced
s1 = xpix[1]-xpix[0]

# the marker size is given as points^2, hence s1**2.
points = ax.scatter(x,y,s=s1**2.,marker='s',edgecolors='none')
ax.axis([min(x)-1., max(x)+1., min(y)-1., max(y)+1.])

fig.savefig('test.png', dpi=fig.dpi)  

However, using this approach, the symbols overlap. I can manually tweak the symbol size so that they align but don't overlap:

s1 = xpix[1]-xpix[0] - 13.  
  • Can the adjustment (in this case the 13) be determined beforehand?
  • What is the flaw in the general approach, requiring an adjustment?
Community
  • 1
  • 1
Schorsch
  • 7,761
  • 6
  • 39
  • 65
  • use `pcolor` instead! It takes care of all of these details for you, is simpler to write, and I suspect faster to render. – tacaswell Jun 03 '13 at 13:02
  • @tcaswell : Thanks for the suggestion. I have come across multiple ways to achieve this. Yet another approach would be `patches`. But in this case I am really simply interested in why this approach doesn't work. – Schorsch Jun 03 '13 at 13:16
  • I was checking the documentation for this at http://matplotlib.org/users/transforms_tutorial.html?highlight=transdata and there was one hint that might be the reason for your problem: "If you run the source code in the example above in a GUI backend, you may also find that the two arrows for the data and display annotations do not point to exactly the same point. This is because the display point was computed before the figure was displayed, and the GUI backend may slightly resize the figure when it is created. (...)" Maybe this helps. – Harpe Jun 04 '13 at 11:39
  • @Harpe : Thanks for this hint. It might be related to something similar. I execute the code from a script, though. But maybe the figure does get resized somewhere in the process. – Schorsch Jun 04 '13 at 11:53

0 Answers0