地质公园图和材料库图有什么区别?为什么不是所有的关键字都可用?
在地质公园有标记,但没有标记色.
在下面的例子中,我用一些造型绘制了一个熊猫df,然后将熊猫df转换成一个地质公园df。简单的绘图工作,但没有额外的样式。
这只是一个例子。在我的地质公园情节中,我想定制,标记,传说等等。我如何访问相关的matplotlib对象?
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
X = np.linspace(-6, 6, 1024)
Y = np.sinc(X)
df = pd.DataFrame(Y, X)
plt.plot(X,Y,linewidth = 3., color = 'k', markersize = 9, markeredgewidth = 1.5, markerfacecolor = '.75', markeredgecolor = 'k', marker = 'o', markevery = 32)
# alternatively:
# df.plot(linewidth = 3., color = 'k', markersize = 9, markeredgewidth = 1.5, markerfacecolor = '.75', markeredgecolor = 'k', marker = 'o', markevery = 32)
plt.show()
# create GeoDataFrame from df
df.reset_index(inplace=True)
df.rename(columns={'index': 'Y', 0: 'X'}, inplace=True)
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['Y'], df['X']))
gdf.plot(linewidth = 3., color = 'k', markersize = 9) # working
gdf.plot(linewidth = 3., color = 'k', markersize = 9, markeredgecolor = 'k') # not working
plt.show()发布于 2022-10-19 07:17:15
您可能对两个库都将方法命名为.plot(这一事实感到困惑。在matplotlib中,具体转换为mpl.lines.Line2D对象,该对象还包含标记及其样式。
Geopandas,假设您想要绘制地理数据,并为此使用路径(mpl.collections.PathCollection)。例如,它有脸和边缘颜色,但没有标记。每当你的路径关闭并形成多边形时,面部颜色就会发挥作用(你的例子不是这样,它只是一条线)。
Geopandas似乎对点/标记使用了一些技巧,它似乎使用"CURVE4“代码(立方Bézier)绘制了一条”路径“。
如果你捕捉到地质公园返回的轴线,你可以探索正在发生的事情:
ax = gdf.plot(...使用ax.get_children(),您将获得所有添加到轴上的艺术家,因为这是一个简单的绘图,很容易看出PathCollection是实际数据。其他艺术家正在画轴线/脊柱等。
[<matplotlib.collections.PathCollection at 0x1c05d5879d0>,
<matplotlib.spines.Spine at 0x1c05d43c5b0>,
<matplotlib.spines.Spine at 0x1c05d43c4f0>,
<matplotlib.spines.Spine at 0x1c05d43c9d0>,
<matplotlib.spines.Spine at 0x1c05d43f1c0>,
<matplotlib.axis.XAxis at 0x1c05d036590>,
<matplotlib.axis.YAxis at 0x1c05d43ea10>,
Text(0.5, 1.0, ''),
Text(0.0, 1.0, ''),
Text(1.0, 1.0, ''),
<matplotlib.patches.Rectangle at 0x1c05d351b10>]如果大量减少点数,比如使用5而不是1024,检索绘制的路径将显示坐标和使用的代码:
pcoll = ax.get_children()[0] # the first artist is the PathCollection
path = pcoll.get_paths()[0] # it only contains 1 Path
print(path.codes) # show the codes used.
# array([ 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
# 4, 4, 4, 4, 4, 4, 4, 4, 79], dtype=uint8)有关这些路径如何工作的更多信息可以在以下站点找到:
https://matplotlib.org/stable/tutorials/advanced/path_tutorial.html
长话短说,您确实拥有与使用Matplotlib时相同的关键字,但它们是用于Path的关键字,而不是您可能期望的Line2D对象。
您可以始终翻转顺序,并从您创建的Matplotlib图形/轴开始,并在您想要绘制某些图形时将这些轴传递给Geopandas。这可能会使您(也)想在相同的轴上绘制其他东西时更容易或更直观。它确实需要更多的纪律来确保(空间)坐标等匹配。
我个人几乎总是这么做的,因为它允许使用相同的Matplotlib API进行大部分绘图,这可能有一个稍微陡峭的学习曲线。但总的来说,我发现它比必须处理每个包的稍微不同的解释要容易一些,这些解释使用了引擎盖下的Matplotlib (例如地质公园、海运、xarray等)。但这真的取决于你从哪里来。
发布于 2022-10-19 08:09:08
谢谢你的详细答复。在此基础上,我从实际的项目中提出了一个简化的代码。
我有一个shapefile shp和一些点数据df,这是我想要绘制的。shp用位势作图,df用matplotlib.plt作图。不需要像我最初所做的那样将点数据传输到geodataframe gdf。
# read marker data (places with coordindates)
df = pd.read_csv("../obese_pct_by_place.csv")
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['sweref99_lng'], df['sweref99_lat']))
# read shapefile
shp = gpd.read_file("../../SWEREF_Shapefiles/KommunSweref99TM/Kommun_Sweref99TM_region.shp")
fig, ax = plt.subplots(figsize=(10, 8))
ax.set_aspect('equal')
shp.plot(ax=ax)
# plot obesity markers
# geopandas, no edgecolor here
# gdf.plot(ax=ax, marker='o', c='r', markersize=gdf['obese'] * 25)
# matplotlib.pyplot with edgecolor
plt.scatter(df['sweref99_lng'], df['sweref99_lat'], c='r', edgecolor='k', s=df['obese'] * 25)
plt.show()https://stackoverflow.com/questions/74120708
复制相似问题