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

Coloring points by categories #244

Closed
lonnylundsten opened this issue Nov 21, 2018 · 8 comments · Fixed by #1006
Closed

Coloring points by categories #244

lonnylundsten opened this issue Nov 21, 2018 · 8 comments · Fixed by #1006
Labels
documentation Improvements or additions to documentation feature request New feature wanted good first issue Good for newcomers
Milestone

Comments

@lonnylundsten
Copy link

Hello,
Thanks for GMT/Python! I am making maps by querying our database for various species and adding points to a basemap -- see attached. I followed the instructions for plotting earthquake magnitude and color coding but that seems to fail with categorical data such as species names. In the attached figure I have a species column in my df with multiple species and I would like to have them plotted as different colored circles on the map. Matplotlib/seaborn etc., allow you to set 'Hue=df.category' for this type of data. Perhaps this could be implemented in gmt-python?
gmt-python-mq0ec2oi

@welcome
Copy link

welcome bot commented Nov 21, 2018

👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.

@lonnylundsten
Copy link
Author

lonnylundsten commented Nov 24, 2018

Not elegant, but I was able to solve this problem by creating an auto-coded color column from my categorical column (conceptname):

#convert conceptname to cetegory and add auto-coded color column
df.conceptname = pandas.Categorical(df.conceptname)
df['colors'] = df.conceptname.cat.codes

Then I was able to use the color column for colors and a standard gmt cmap for my colormap:

#plot df lat/lon points on map
fig.plot(x=df.longitude, y=df.latitude, color=df.colors.astype('int64', copy=False), cmap='rainbow',style='c0.2c',pen='black')

@lonnylundsten
Copy link
Author

lonnylundsten commented Dec 7, 2018

The bolded bits in this code plot the scale bar (L flag) and directional rose (Td flag).

#set gmt region, projection, and map view
fig.coast(region=df_region,projection='M-120/42/14', frame=True,land='darkgrey', water='lightgrey', shorelines=0,borders='a',**L='g-128/23.8+c-120/40+w500k', Td='g-130/26+w2c'**)

This info is listed in the API reference:
https://www.gmtpython.xyz/latest/api/index.html

Note that each heading in the API reference is hyperlinked to further info, ie:
https://www.gmtpython.xyz/latest/api/generated/gmt.Figure.basemap.html#gmt.Figure.basemap

And then that link will direct you to the official GMT docs for even more info.
http://gmt.soest.hawaii.edu/doc/latest/basemap.html

@leouieda
Copy link
Member

@lonnylundsten sorry for the delay. Were you able to plot it in the end?

Do you want the colors to be taken from a colormap or just use different colors? If you just want some different colors, this is something that doesn't work well in GMT. I think you need to do it manually currently. There is some talk of implementing this in the base GMT but it might take a while. We could probably implement this on the Python side in the mean time. I'll have to think of a good way to do it.

@leouieda leouieda added the feature request New feature wanted label Jan 22, 2019
@lonnylundsten
Copy link
Author

Yes, I was able to implement this using the code above. I created my own color map, which worked great. Thanks!

gmt-python-kfzhazrx.pdf

@lonnylundsten
Copy link
Author

gmt-python-kfzhazrx.pdf

@lonnylundsten
Copy link
Author

Oops. I did not mean to close this. I'm at sea and the internet is being laggy!
Now reopened.
LL

@weiji14 weiji14 mentioned this issue Oct 27, 2020
5 tasks
@weiji14 weiji14 added the documentation Improvements or additions to documentation label Nov 22, 2020
@weiji14 weiji14 added the good first issue Good for newcomers label Feb 23, 2021
@weiji14
Copy link
Member

weiji14 commented Feb 23, 2021

To close this >2 year old issue, I think it would be great if someone can add an example to our gallery that demonstrates how to "Color points by categories". The 3D Scatter Plot example actually covers this, but it would be nice to have a simpler, 2D example.

One way would be to just copy and modify https://www.pygmt.org/v0.3.0/gallery/plot/scatter.html, and instead of using a for-loop, use fig.plot(..., color=df.color_column.cat.codes.astype(int), ...) instead. Let us know (anyone) if you're interested in helping, have a look at CONTRIBUTING.md, and we'll be happy to guide you through!

michaelgrund added a commit that referenced this issue Mar 4, 2021
To close the >2 year old issue #244 here's a gallery example highlighting the coloring of points by categories.
michaelgrund added a commit that referenced this issue Mar 4, 2021
To close the >2 year old issue #244  here's a gallery example highlighting the coloring of points by categories.
@michaelgrund michaelgrund linked a pull request Mar 4, 2021 that will close this issue
3 tasks
@seisman seisman added this to the 0.3.1 milestone Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature request New feature wanted good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants