Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

python - Scatter plot only lines that meet the condition in a specific column

I would like to scatter plot a number of stations from a txt file to a map, using cartopy:

def ReadData(FileName,typee,delimee):
    return np.genfromtxt(FileName, dtype=typee, delimiter=delimee, encoding='latin-1')

MyTypes = ("|U11","float","float","float","|U1","|U2","|U1","|U29")
MyDelimiters = [11,9,10,7,1,2,1,29] # station ID, lat, lon (-180 to 180), elevation (m), blank, Country code, blank, Name
RawData = ReadData(stations.txt,MyTypes,MyDelimiters)

stations.txt:

01001099999  70.9330   -8.6670    9.0 NO JAN MAYEN(NOR-NAVY)           0100109
01001599999  61.3830    5.8670  327.0 NO BRINGELAND                    0100159
01003099999  77.0000   15.5000   12.0 NO HORNSUND                      0100309
01008099999  78.2460   15.4660   26.8 SV LONGYEAR                      0100809
01010099999  69.2930   16.1440   13.1 NO ANDOYA                        0101009 

2nd column represents the latitudes, 3rd column the longitudes, 4th column the elevation.

StationListID  = np.array(RawData['f0'])
StationListLat  = np.array(RawData['f1'])
StationListLon  = np.array(RawData['f2'])
StationListElev = np.array(RawData['f3'])

I use:

import matplotlib.pyplot as plt
import cartopy.crs as crs

plt.scatter(x=StationListLon, y=StationListLat,
                color="dodgerblue",
                s=1,
                alpha=0.5,
                transform=crs.PlateCarree())

If the elevation < 0, I would like to have black dots, for > 5 green, for > 10 red and for > 15 blue dots. Where do I set the if conditions or group the lines?

question from:https://stackoverflow.com/questions/65890366/scatter-plot-only-lines-that-meet-the-condition-in-a-specific-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Modules and data:

import numpy as np
import pandas as pd
import io

RawData = pd.read_csv(io.StringIO("""
x f1 f2 f3 stations
01001099999 70.9330 -8.6670 9.0 NOJANMAYEN(NOR-NAVY) 
01001599999 61.3830 5.8670 327.0 NOBRINGELAND          
01003099999 77.0000 15.5000 12.0 NOHORNSUND             
01008099999 78.2460 15.4660 26.8 SVLONGYEAR             
01010099999 69.2930 16.1440 13.1 NOANDOYA
"""), sep="s", engine="python")

StationListLat  = np.array(RawData['f1'])
StationListLon  = np.array(RawData['f2'])
StationListElev = np.array(RawData['f3'])

You could first make labels that signify the colors using pd.cut.

color_labels = ['black', 'yellow', 'green', 'red', 'blue']
cut_bins = [-500, 0, 5, 10, 15, 500]
RawData['colors'] = pd.cut(RawData['f3'], bins=cut_bins, labels=color_labels)

Then you could use these labels to display the colors of the dots. Note that you do not have a color for values inbetween 0 and 5, I just gave it the color yellow. As you see, I left the crs part out, if I am not mistaken it is not directly relevant for this problem.

import matplotlib.pyplot as plt

plt.scatter(x=StationListLon, y=StationListLat,
            color=RawData['colors'],
            s=20,
            alpha=0.5)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...