-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Legend for Scatter #11127
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
Merged
efiring
merged 1 commit into
matplotlib:master
from
ImportanceOfBeingErnest:legend-for-scatter
Feb 12, 2019
Merged
Legend for Scatter #11127
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
:orphan: | ||
|
||
Legend for scatter | ||
------------------ | ||
|
||
A new method for creating legends for scatter plots has been introduced. | ||
Previously, in order to obtain a legend for a :meth:`~.axes.Axes.scatter` | ||
plot, one could either plot several scatters, each with an individual label, | ||
or create proxy artists to show in the legend manually. | ||
Now, :class:`~.collections.PathCollection` provides a method | ||
:meth:`~.collections.PathCollection.legend_elements` to obtain the handles and labels | ||
for a scatter plot in an automated way. This makes creating a legend for a | ||
scatter plot as easy as:: | ||
|
||
scatter = plt.scatter([1,2,3], [4,5,6], c=[7,2,3]) | ||
plt.legend(*scatter.legend_elements()) | ||
|
||
An example can be found in | ||
:ref:`automatedlegendcreation`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this too confusing. What does
*func*
do and why do you need it? Maybe this needs an example...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func
would allow to calculate the values to be shown in the legend. This is mainly useful for "sizes", because unlike thec
parameter in scatter, which takes numbers and maps them to color using aNormalize
, thes
parameter does not have a normalization. This means that usually you would encode your data into sizes and provide those sizes in the scatter call. However in the legend you might still want to show the original data's values. Example: You use thes
to show price ranging between 0.5 and 10 dollars. Now you cannot use those numbers directly as scatter sizes, because that would result in tiny dots. So you convert them to sizes vias = lambda price : 0.5*(price*10)^2
. Nowfunc
would allow you to provideprice = lambda s: np.sqrt(2*s)/10
to have a nice legend using the actual prices in dollars.I think you are right that an example might be useful here. I could add that in addition to the already extended example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I was particularly flummoxed by "This converts the initial values for color or size and needs to take a numpy array as input." I wasn't sure what "initial" meant.
For the doc string maybe: "Function to calculate the labels. Often the size (or color) argument to scatter will have been pre-processed by the user using a function
s = f(x)
to make the markers visible; egsize = np.log10(x)`. Providing this function allows that pre-processing to be inverted, so that the legend labels have the correct values. eg
np.exp(x, 10)``"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reformulated the docstring and also added an additional example now.