首页 > 其他分享 >[1061] Plotting a GeoDataFrame by matplotlib

[1061] Plotting a GeoDataFrame by matplotlib

时间:2024-09-12 09:47:33浏览次数:9  
标签:plot set GeoDataFrame 1061 subplots plt gdf ax Plotting

import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString, Polygon

# Create sample geometries
points = [Point(.5, .5), Point(1.5, 1.5), Point(2.5, 2.5), Point(3.5, 3.5), Point(5, 5)]
lines = [LineString([(5, 0), (5, 4), (2, 4)]), LineString([(0, 0), (1, 2), (1.5, 2)])]
polygons = [Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]), Polygon([(2, 2), (3, 2), (3, 3.5), (2, 3)])]

# Create a GeoDataFrame
gdf = gpd.GeoDataFrame({'geometry': points + lines + polygons})

# Separate GeoDataFrame by geometry type
points_gdf = gdf[gdf.geometry.type == 'Point']
lines_gdf = gdf[gdf.geometry.type.isin(['LineString', 'MultiLineString'])]
polygons_gdf = gdf[gdf.geometry.type.isin(['Polygon', 'MultiPolygon'])]

# Plot each geometry type with different styles
fig, ax = plt.subplots()

# The last one is the top layer
# The layer overlaps one by one
polygons_gdf.plot(ax=ax, color='blue', edgecolor='black', alpha=0.5, label='Polygons')
lines_gdf.plot(ax=ax, color='green', linewidth=2, label='Lines')
points_gdf.plot(ax=ax, color='red', markersize=25, label='Points')

# Customize the plot
ax.set_title('GeoDataFrame with Multiple Geometries')
ax.legend()
plt.show()

In this example:

  • We create multiple points, lines, and polygons.
  • We combine them into a single GeoDataFrame.
  • We then separate the GeoDataFrame into different geometry types.
  • Finally, we plot each geometry type with different styles and add a legend to distinguish them.

plt.subplots is a convenient way to create a figure and a set of subplots in Matplotlib. It returns a tuple containing a figure and an array of axes objects. Here’s a basic example to illustrate how it works:

import matplotlib.pyplot as plt

# Create a figure and a set of subplots
fig, ax = plt.subplots()

# Plot data on the axes
ax.plot([1, 2, 3, 4], [10, 20, 25, 30])

# Customize the plot
ax.set_title('Simple Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

# Show the plot
plt.show()

Creating Multiple Subplots

You can also create multiple subplots in a grid layout. For example, to create a 2x2 grid of subplots:

import matplotlib.pyplot as plt

# Create a 2x2 grid of subplots
fig, axs = plt.subplots(2, 2)

# Plot data on each subplot
axs[0, 0].plot([1, 2, 3, 4], [10, 20, 25, 30])
axs[0, 0].set_title('Plot 1')

axs[0, 1].plot([1, 2, 3, 4], [30, 25, 20, 10])
axs[0, 1].set_title('Plot 2')

axs[1, 0].plot([1, 2, 3, 4], [15, 18, 22, 27])
axs[1, 0].set_title('Plot 3')

axs[1, 1].plot([1, 2, 3, 4], [5, 15, 10, 20])
axs[1, 1].set_title('Plot 4')

# Adjust layout to prevent overlap
plt.tight_layout()

# Show the plot
plt.show()

Customizing Subplots

You can customize each subplot individually by accessing the corresponding axes object. For example, you can set titles, labels, and other properties for each subplot.

Would you like to see more specific examples or details on customizing subplots?

标签:plot,set,GeoDataFrame,1061,subplots,plt,gdf,ax,Plotting
From: https://www.cnblogs.com/alex-bn-lee/p/18409582

相关文章

  • [1060] Create the unique ID from the index (DataFrame, GeoDataFrame)
    Thereareseveralwaystoimplementit!Hereisasampledataset:importpandasaspd#SampleDataFramedf=pd.DataFrame({'A':[1,2,3,4],'B':[None,5,None,7]})1.pd.Series()#ConverttheindextoaSerieslikeac......
  • 如何在闪亮的应用程序中从geodataframe下载geopackage文件?
    我有一个shiny应用程序,它使用folium在地图上显示多个图层。我想让用户能够将其中一个图层(线串地理数据框)下载为地理包文件。这是我到目前为止的代码:#relevantimportedpackagesfromshinyimportApp,render,ui,reactiveimportpandasaspdimport......
  • [题解]CF1061C Multiplicity
    题意给定一个长度为\(n\)的序列\(\{a_1,a_2,\dots,a_n\}\)。现在要在\(a\)选出非空子序列\(\{b_1,b_2,\dots,b_m\}\),使得所有的\(i\in[1,m]\),都有\(b_i\bmodi=0\)。求能够选取\(b\)序列的方案数模\(10^9+7\)的值。思路定义\(dp_{i,j}\)表示在\(\{a_1,a......
  • CSP历年复赛题-P1061 [NOIP2006 普及组] Jam 的计数法
    原题链接:https://www.luogu.com.cn/problem/P1061题意解读:从编号s~t的字母从挑w个,组成一种特殊的数字,数字里字母都是升序的,给定初始数字,要计算后5个。解题思路:1、模拟法模拟样例:2105有效字母范围:b,c,d,e,f,g,h,i,j 初始值:bdfij要计算bdfij的下一个,采取如下步骤......
  • [969] Add a spatial reference (a coordinate reference system, CRS) to a GeoDataF
    Toaddaspatialreference(acoordinatereferencesystem,CRS)toaGeoDataFrameinGeoPandas,youcansetthecrsattributeoftheGeoDataFrametothedesiredCRS.Here'showyoucandoit:importgeopandasasgpdfromshapely.geometryimportPoint......
  • [964] Convert a DataFrame to a GeoDataFrame
    ToconvertaDataFrametoaGeoDataFrameinPandas,youcanusethegeopandas.GeoDataFrameconstructorandprovidethegeometrycolumn.Here'sanexample:importpandasaspdimportgeopandasasgpdfromshapely.geometryimportPoint#SampleDataFr......
  • 无涯教程-MATLAB - 绘图(Plotting)
    要绘制函数图,您需要执行以下步骤-通过为变量x指定值的范围定义x,为此函数绘制定义函数y=f(x)以plot(x,y)以下示例将演示该概念。让我们用简单的函数y=x绘制x的值范围(从0到100),增量为5。创建一个脚本文件并输入以下代码-x=[0:5:100];y=x;plot(x,y)运行文件时,MAT......
  • [936] Save a GeoDataFrame as a Shapefile
    InGeoPandas,youcansaveaGeoDataFrameasaShapefileusingtheto_filemethod.Here'showtodoit:importgeopandasasgpd#CreateorloadaGeoDataFrame(gdf)thatyouwanttosave#...#SpecifythepathandnamefortheShapefile(e.g.,&......
  • Failed to transform tygerservice-1.0.0.210619103852.aar to match attributes
    Couldnotresolveallfilesforconfiguration':app:debugRuntimeClasspath'.ExecutionfailedforAarToClassTransform:C:\Users\Administrator\.gradle\caches\transforms-2\Failedtotransformtygerservice-1.0.0.210619103852.aartomatchattrib......
  • 【847】create geoDataFrame from dataframe
    Ref:FromWKTformatFirstly,wealreadyhaveadataframe,andthereisacolumnofgeometry.Butthiscolumnisintheformatofthestring,therefore,weshouldchangethedataformatfromthestringtothepolygon.Therearetwowaystoimplementthis......