3

My question is as far as I see closely related to this post.

I need to plot some data with marker size strictly proportional to the value of the axes. (Already asked the question here).

My approach is the following:

  • Create empty scatterplot for pixel reference
  • Scatter 2 points on the lower left and upper right corner
  • Limit the axes to exactly these two points
  • use transData.transform to get the pixel values of those two points
  • get the pixel distance as the difference in pixel number of these two points
  • (now that I have the distance-to-pixel-ratio, scatter my data with
    s=(size*dist_to_pix_ratio)**2; but that is not important right now.)

Problem is: when I do exactly what I've described, I get two different values for the pixel number for the y-axis and the x-axis.

Here is a minimal code:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(7,7))
ax1 = fig.add_subplot(111, aspect='equal')

#setting up an empty scatterplot for pixel reference
xedges=[0.0, 1.0]
yedges=[0.0, 1.0]
emptyscatter=ax1.scatter(xedges, yedges, s=0.0)

#set axes limits
ax1.set_xlim(0.00,1.00)
ax1.set_ylim(0.00,1.00)   


# Calculating the ratio of pixel-to-unit
upright = ax1.transData.transform((1.0,1.0))
lowleft = ax1.transData.transform((0.0,0.0))
x_to_pix_ratio = upright[0] - lowleft[0]
y_to_pix_ratio = upright[1] - lowleft[1]

print x_to_pix_ratio, y_to_pix_ratio

which returns:

434.0 448.0

Can anybody explain why they are not equal?

I'm not sure if it's relevant, but I'm using Python 2.7.12 and matplotlib 1.5.1

taras
  • 6,566
  • 10
  • 39
  • 50
mivkov
  • 471
  • 5
  • 19
  • Additional information: I have found that the values `x_to_pix_ratio` and `y_to_pix_ratio` as well as their ratio depend on the `figsize` parameter. (Obviously one would expect the actual numbers to vary with figsize, but the ratio?) – mivkov Nov 06 '16 at 02:09
  • 1
    Do a `fig.canvas.draw()` before you calculate the ratio of pixel-to-unit and you should get 434 pixels for each axis. – Jean-Sébastien Nov 06 '16 at 03:09
  • 1
    @tacaswell could explain it better than me, but some stuffs are only calculated when the figure is being drawn. So sometimes, it is necessary to call a `fig.canvas.draw()` before doing some pixel related calculations in matplotlib. – Jean-Sébastien Nov 06 '16 at 03:29
  • @Mladen, It works fine for me in Python `3.5.2` and matplotlib `1.5.3`. So, the code you've written is correct. – Nickil Maveli Nov 06 '16 at 14:36
  • 1
    @Jean-Sébastien That worked perfectly, thanks! – mivkov Nov 06 '16 at 18:15

0 Answers0