【问题标题】:Scatter plot only lines that meet the condition in a specific column散点图仅在特定列中满足条件的行
【发布时间】:2021-04-29 14:07:54
【问题描述】:

我想使用 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())

如果海拔 5 绿色,> 10 红色和 > 15 蓝点。在哪里设置 if 条件或对行进行分组?

【问题讨论】:

  • 我的回答对你有帮助吗?
  • 非常感谢您的帮助,Ruthger!你的代码有效,是的。我正在通过RawData = ReadData(InList,MyTypes,MyDelimiters) 读取数据,RawData['colors'] = pd.cut(RawData['f3'], bins=cut_bins, labels=color_labels) 给了我ValueError: no field of name colors
  • 很难看出发生了什么。您能否更新您的问题并添加您采取的所有步骤并显示RawData.head() 在每个步骤之后给出的内容?
  • 也许已经存在错误:RawData.head() 就在StationListElev = np.array(RawData['f3']) 给我*** AttributeError: 'numpy.ndarray' object has no attribute 'head' 之后。我的 RawData 看起来像这样,如果我不将 x f1 f2 f3 站添加到我的 ASCII 文件的第一行:[('01001099999', 70.933, -8.667, 9. , ' ', 'NO', ' ', 'JAN MAYEN(NOR-NAVY) ') ... ("''''\n", nan, nan, nan, '', '', '', '')]
  • type(RawData)ReadData 之后和StationListElev = np.array(RawData['f3']) 之后直接给你什么?

标签: python python-3.x colors scatter-plot


【解决方案1】:

模块和数据:

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)

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 2014-02-18
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多