我正在尝试找到绘制一些数据的最佳方法。基本上我有一个数据文件,其中包含纬度、经度、深度、sample_ID、Group_ID 列。我想生成一个二维散点图,其中 y 是深度,x 是从北到南的距离(以公里为单位)(或者相对于在指示方向采样的第一个站点计算横断面距离),类似于 ODV 样式 map ,例如下面这个:
已更新
我想在我最初的问题中添加更多信息。经过更多搜索和测试,我在 R 中找到了一个可能的解决方案,使用 geosphere 包和 distGEO 函数将坐标转换为以 km 为单位的距离,然后可以进行映射。 (https://www.rdocumentation.org/packages/geosphere/versions/1.5-10/topics/distGeo)
如果有人知道 python 的方法来做到这一点那就太好了!
已更新
ODV 不允许我进行我需要的定制。我想生成这样的图,我可以在其中指定元数据变量来为点着色。更具体地说,我的数据文件中的 group_ID 列如下面的文件示例所示。
Latitude Longitude Depth_m Sample_ID Group_ID
49.7225 -42.4467 10 S1 1
49.7225 -42.4467 50 S2 1
49.7225 -42.4467 75 S3 1
49.7225 -42.4467 101 S4 1
49.7225 -42.4467 152 S5 1
49.7225 -42.4467 199 S6 1
46.312 -39.658 10 S7 2
46.312 -39.658 49 S8 2
46.312 -39.658 73 S9 2
46.312 -39.658 100 S10 2
46.312 -39.658 153 S11 2
46.312 -39.658 198 S12 2
尽管如此,它给我带来了很多麻烦。我已经使用半正弦计算计算了坐标之间的距离,但是一旦到达那里,我不确定如何使用这些距离将其合并到散点图中。这是我到目前为止所拥有的:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#import haversine as hs
from math import radians
from sklearn.neighbors import DistanceMetric
df=pd.read_csv("locations.csv",sep="\t",index_col="Sample_ID")
#plt.scatter(df['Latitude'], df['Depth_m'])
#plt.show()
df['Latitude'] = np.radians(df['Latitude'])
df['Longitude'] = np.radians(df['Longitude'])
dist = DistanceMetric.get_metric('haversine')
x = dist.pairwise(np.unique(df[['Latitude','Longitude']].to_numpy(),axis=0))*6373
print(x)
这段代码为我提供了一个坐标距离矩阵,但老实说,我不知道如何将其拉入一个从北到南设置 x 轴的散点图。特别是因为必须考虑具有相同坐标的多个深度。非常感谢任何帮助绘图!
最佳答案
对于距离计算,您可以使用geopy包,特别是geopy.distance.geodesic(),通过假设特定的椭球来计算沿弧线的距离(例如 WGS84)。
要生成与您所描述的类似的绘图,您可以使用 matplotlib 库的散点图功能,特别是 matplotlib.pyplot.scatter()。
下面的代码示例将引导您完成距离计算(从某个引用纬度/经度到另一个纬度/经度的距离...这不一定是 N-S 分量,但很容易计算)。以及如何使用 Group_ID 字段生成散点图,并使用两种方法为点着色。
import matplotlib.pyplot as plt
import geopy
import pandas as pd
# Load your sample data to a Pandas DataFrame where each column corresponds to
# 'Latitude', 'Longitude', 'Depth_m', 'Sample_ID', 'Group_ID'
datafile = r'<path to a file containing your data>'
df = pd.read_csv(datafile)
# Defining one end of our arc to calculate distance along (arbitrarily taking
# the first point in the example data as the reference point).
ref_point = (df['Latitude'].iloc[0], df['Longitude'].iloc[0])
# Loop over each sample location calculating the distance along the arc using
# pygeo.distance.geodesic function.
dist = []
for i in range(len(df)):
cur_point = (df['Latitude'].iloc[i], df['Longitude'].iloc[i])
cur_geodesic = geopy.distance.geodesic(ref_point, cur_point)
cur_dist = cur_geodesic.km
dist.append(cur_dist)
# Add computed distances to the df DataFrame as column 'Distance_km'
df['Distance_km'] = dist
# Create a matplotlib figure and add two axes for plotting
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
# Example 1: of creating a scatter plot using the calculated distance field and
# colouring the points using a numeric field (i.e. Group_ID in this case is numeric)
pts = ax1.scatter(df['Distance_km'], df['Depth_m'], s=30, c=df['Group_ID'], cmap=plt.cm.jet)
plt.colorbar(pts, ax=ax1)
ax1.set_xlabel('Arc Distance from Reference Point (km)')
ax1.set_ylabel('Depth (m)')
ax1.set_title('Colouring Points by a Numeric Field')
ax1.invert_yaxis()
ax1.grid(True)
# Example of creating basically the same scatter plot as above but handling the
# case of non-numeric values in the field to be used for colour (e.g. imagine
# wanting to the the Sample_ID field instead)
groups = list(set(df['Group_ID'])) # get a list of the unique Group_ID values
for gid in groups:
df_tmp = df[df['Group_ID'] == gid]
ax2.scatter(df_tmp['Distance_km'], df_tmp['Depth_m'], s=30, label=gid)
ax2.legend(loc='upper center', title='Legend')
ax2.set_xlabel('Arc Distance from Reference Point (km)')
ax2.set_ylabel('Depth (m)')
ax2.set_title('Colouring Points with Using Categorical Values')
ax2.invert_yaxis()
ax2.grid(True)
fig.tight_layout()
plt.show()
关于python - 在python3中绘制散点图,其中x轴是纬度/经度(以公里为单位),y轴是深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65797219/