You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may be either a bug report or a feature request, depending how you view things. The issue is that you cannot use a factor/string column in a pandas dataframe as color option without awkward workarounds.
Code for reproduction
data = pd.DataFrame(data={"x":[1,2,3,2,4], "y":[2,1,2,3,1], "z":["a", "b", "b", "a", "c"]})
plt.scatter(x="x", y="y", data=data) # This works as nicely and as expected
plt.scatter(x="x", y="y", c="z", data=data) # This throws the following error:
Actual outcome
ValueError: c of shape (5,) not acceptable as a color sequence for x with size 5, y with size 5
Expected outcome
An image where each point is colored according to it's value in "z", cycling through a predefined color map. A shining example of how easy such a commonly used visualization should be is ggplot, see the code in this post: The color option automatically notices that this is a factor variable and assigns each level a color. Also, a legend is automatically shown.
After checking the documentation again, this is definitely a feature request. After seeing how many posts on stackoverflow are about exactly this problem, I think this is something that is really not working as expected - Intuition (and implementations in R and, for example, seaborn), suggest that matplotlib should automatically deduce what the user wants in this case instead of throwing an error.
Yes, as long as it's numerical, it works. Only the implementation for
string / categorical data is missing.
Jody Klymak <notifications@github.com> schrieb am Di., 12. Juni 2018, 18:21:
However, this also plots on a scale of continuous numbers instead of using
categories. Implementation of this in a useful way (like ggplot & seaborn
do it) might be harder than initially thought.
Thomas <teefkay@gmail.com> schrieb am Di., 12. Juni 2018, 18:26:
Yes, as long as it's numerical, it works. Only the implementation for
string / categorical data is missing.
Jody Klymak ***@***.***> schrieb am Di., 12. Juni 2018,
18:21:
> OK, if you do data={"x":[1,2,3,2,4], "y":[2,1,2,3,1], "z":[5, 4, 3, 2,
> 1]} this works, right? We don't have categorical data for the z axis.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#11422 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/APirBH4xsIj8KjL_XqhWmDJtz6JUX5Tpks5t7-qXgaJpZM4Uki7y>
> .
>
Yes, right now categorical axes work by converting the string "units" to numbers. But we don't have the equivalent unit conversion machinery in place for z-data. I think one of the reasons seaborn is around is to scratch that itch. We are thinking about how to do it, lead by @story645, but its not trivial.
Concerning the legend, there is a proposal #11127 which is still under review to simplify the legend creation. If this passes as in its current proposed version, a legend could be simply created like
plt.legend(scatter.legend_elements()[0],labels)
However, as of now you would need to create the legend manually, e.g. like
handle = lambda c : plt.Line2D([0],[0], ls="", marker="o", color=scatter.cmap(scatter.norm(c)))
handles = [handle(c) for c in np.unique(data.znum)]
plt.legend(handles, labels)
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
kasuteru commentedon Jun 12, 2018
After checking the documentation again, this is definitely a feature request. After seeing how many posts on stackoverflow are about exactly this problem, I think this is something that is really not working as expected - Intuition (and implementations in R and, for example, seaborn), suggest that matplotlib should automatically deduce what the user wants in this case instead of throwing an error.
jklymak commentedon Jun 12, 2018
OK, if you do
data={"x":[1,2,3,2,4], "y":[2,1,2,3,1], "z":[5, 4, 3, 2, 1]}this works, right? We don't have categorical data for the z axis.kasuteru commentedon Jun 12, 2018
kasuteru commentedon Jun 12, 2018
jklymak commentedon Jun 12, 2018
Yes, right now categorical axes work by converting the string "units" to numbers. But we don't have the equivalent unit conversion machinery in place for z-data. I think one of the reasons seaborn is around is to scratch that itch. We are thinking about how to do it, lead by @story645, but its not trivial.
ImportanceOfBeingErnest commentedon Jun 12, 2018
This is how I would currently do it:
Concerning the legend, there is a proposal #11127 which is still under review to simplify the legend creation. If this passes as in its current proposed version, a legend could be simply created like
However, as of now you would need to create the legend manually, e.g. like
ImportanceOfBeingErnest commentedon Jun 13, 2018
Xref: This request is also very similar to the request in #6214.
kasuteru commentedon Jun 13, 2018
@ImportanceOfBeingErnest : That indeed seems to be the same issue, with broader scope.
github-actions commentedon Oct 11, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
story645 commentedon Oct 11, 2023
I think this is still wanted and I think one way to tackle this is to try and make NoNorm unit aware #7383
github-actions commentedon Oct 11, 2024
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!