Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example code misleading: examples/api/scatter_piecharts.py scaled incorrectly #3441

Closed
epipping opened this issue Aug 31, 2014 · 5 comments
Closed
Milestone

Comments

@epipping
Copy link
Contributor

In the example scatter_piecharts.py, segments of the unit disk are approximated through polygons and used as markers. By chance, each segment has a coordinate with modulus 1, so that it is not rescaled. As soon as one starts to play with the example a bit, e.g. changing the proportions of the first two segments into

r1 = 0.1
r2 = r1 + 0.05

the second, green segment is rescaled, which it should not be for our purposes.

incorrectly_scaled

To counter that effect, one needs to apply a segment-specific scale, as is done here:

correctly_scaled

The resulting code looks like this (there's probably a nicer waying of writing this):

"""
This example makes custom 'pie charts' as the markers for a scatter plotqu

Thanks to Manuel Metz for the example
"""
import math
import numpy as np
import matplotlib.pyplot as plt

# first define the ratios
r1 = 0.2      # 20%
r2 = r1 + 0.4 # 40%

# define some sizes of the scatter marker
sizes = [60,80,120]

# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2*math.pi*r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2*math.pi*r1, 10)).tolist()
xy1 = list(zip(x,y))
s1 = max(max(x),max(y))

# ...
x = [0] + np.cos(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r1, 2*math.pi*r2, 10)).tolist()
xy2 = list(zip(x,y))
s2 = max(max(x),max(y))

x = [0] + np.cos(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2*math.pi*r2, 2*math.pi, 10)).tolist()
xy3 = list(zip(x,y))
s3 = max(max(x),max(y))

fig, ax = plt.subplots()
ax.scatter( np.arange(3), np.arange(3), marker=(xy1,0),
            s=[ s1*s1*x for x in sizes ], facecolor='blue' )
ax.scatter( np.arange(3), np.arange(3), marker=(xy2,0),
            s=[ s2*s2*x for x in sizes ], facecolor='green' )
ax.scatter( np.arange(3), np.arange(3), marker=(xy3,0),
            s=[ s3*s3*x for x in sizes ], facecolor='red' )
plt.show()
@tacaswell tacaswell added this to the v1.4.x milestone Sep 1, 2014
@tacaswell
Copy link
Member

Can you turn this into a PR? It makes it much easier to understand the changes.

On a quick scan, this seems fine.

@tacaswell
Copy link
Member

Sorry, I speak in jargon too much. By PR I mean 'pull request'.

@epipping
Copy link
Contributor Author

epipping commented Sep 1, 2014

Should I open a separate PR or does the above reference help?

@tacaswell
Copy link
Member

Open a separate PR please.

@tacaswell
Copy link
Member

Fixed in #3448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants