Skip to content

Problem with scatter size when set xlim/ylim #16548

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

Closed
GYHHAHA opened this issue Feb 18, 2020 · 10 comments
Closed

Problem with scatter size when set xlim/ylim #16548

GYHHAHA opened this issue Feb 18, 2020 · 10 comments
Labels
Community support Users in need of help.

Comments

@GYHHAHA
Copy link

GYHHAHA commented Feb 18, 2020

fig = plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(1,1) 
ax0 = plt.subplot(gs[0])
ax0.scatter([0],[1],s=20000)
ax0.scatter([0],[3],s=20000)
ax0.set_xlim(-2,2)
ax0.set_ylim(-5,5)

This result of the above code shows two intersecting circles.
bug1
But if I cancel the set_xlim/set_ylim step, these two circles are separated.

fig = plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(1,1) 
ax0 = plt.subplot(gs[0])
ax0.scatter([0],[1],s=20000)
ax0.scatter([0],[3],s=20000)

bug2
So how to find a more explicit way to decide the size of scatters?
Thanks!

@GYHHAHA
Copy link
Author

GYHHAHA commented Feb 18, 2020

I find the problem can be solved in this way.
Still Thanks!

@GYHHAHA GYHHAHA closed this as completed Feb 18, 2020
@story645
Copy link
Member

Is there a reason you can't set s to a different value, like s=10000?

@timhoffm
Copy link
Member

For future reference: Only marker positions are included in auto-scaling; marker sizes are not. For really large markers this can lead to observed clipping.

@GYHHAHA
Copy link
Author

GYHHAHA commented Feb 18, 2020

Whether exists a more convenient way to scatter with a given radius? (suppose it's a circle marker)

@GYHHAHA
Copy link
Author

GYHHAHA commented Feb 18, 2020

Is there a reason you can't set s to a different value, like s=10000?

Actually a different value can be set.

@tacaswell
Copy link
Member

@GYHHAHA Depending on what you are doing, you may be better off using https://matplotlib.org/api/_as_gen/matplotlib.patches.Circle.html It is a bit fussier to use (you will have to create the artists and add them to the axes your self), but it will let you control the size of the circles in data space (rather than in pixel space).

@timhoffm
Copy link
Member

An alternative workaround is to impose larger margins around the data: plt.rcParams['axes.ymargin'] = 0.2 when you know, you have very large markers.

@GYHHAHA
Copy link
Author

GYHHAHA commented Feb 19, 2020

Indeed, it's more convenient to apply circle under this situation. Thanks. @tacaswell

@ImportanceOfBeingErnest
Copy link
Member

In addition to the options shown in the SO post OP linked to, probably the fastest option would be to use a PathCollection.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as mcoll
import matplotlib.transforms as mtrans
import matplotlib.markers as mmarkers

c = mcoll.PathCollection(
                [mmarkers.MarkerStyle("o").get_path()],
                offsets=np.repeat(np.arange(10),2).reshape(10,2),
                edgecolors=["k"]*10,
                transOffset=mtrans.IdentityTransform(),
                offset_position="data")
plt.gca().add_collection(c)
plt.gca().set(xlim=(-2, 11), ylim=(-2,11))
plt.gca().set_aspect("equal")
plt.show()

I think in view of this, it's relevant to discuss the possibility to either not deprecate offset_position="data" or to provide a public replacement option.

@GYHHAHA
Copy link
Author

GYHHAHA commented Feb 24, 2020

Yes, it seems more natural. Thanks for your solution! @ImportanceOfBeingErnest

@QuLogic QuLogic added the Community support Users in need of help. label Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community support Users in need of help.
Projects
None yet
Development

No branches or pull requests

6 participants