散点图仅绘制特定列中满足条件的线
Scatter plot only lines that meet the condition in a specific column
我想使用 cartopy 从 txt 文件到地图散点图:
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
第 2 列代表纬度,第 3 列代表经度,第 4 列代表海拔。
StationListID = np.array(RawData['f0'])
StationListLat = np.array(RawData['f1'])
StationListLon = np.array(RawData['f2'])
StationListElev = np.array(RawData['f3'])
我使用:
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())
如果海拔 < 0,我想要黑点,> 5 个绿色点,> 10 个红色点和 > 15 个蓝色点。在哪里设置 if 条件或对行进行分组?
模块和数据:
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'])
您可以先使用 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)
然后您可以使用这些标签来显示点的颜色。请注意,您没有 0 到 5 之间的值的颜色,我只是给它设置了黄色。
如您所见,我省略了 crs 部分,如果我没记错的话,它与这个问题没有直接关系。
import matplotlib.pyplot as plt
plt.scatter(x=StationListLon, y=StationListLat,
color=RawData['colors'],
s=20,
alpha=0.5)
我想使用 cartopy 从 txt 文件到地图散点图:
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
第 2 列代表纬度,第 3 列代表经度,第 4 列代表海拔。
StationListID = np.array(RawData['f0'])
StationListLat = np.array(RawData['f1'])
StationListLon = np.array(RawData['f2'])
StationListElev = np.array(RawData['f3'])
我使用:
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())
如果海拔 < 0,我想要黑点,> 5 个绿色点,> 10 个红色点和 > 15 个蓝色点。在哪里设置 if 条件或对行进行分组?
模块和数据:
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'])
您可以先使用 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)
然后您可以使用这些标签来显示点的颜色。请注意,您没有 0 到 5 之间的值的颜色,我只是给它设置了黄色。 如您所见,我省略了 crs 部分,如果我没记错的话,它与这个问题没有直接关系。
import matplotlib.pyplot as plt
plt.scatter(x=StationListLon, y=StationListLat,
color=RawData['colors'],
s=20,
alpha=0.5)