In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to draw vector data in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1 Vector data drawing
Learning objectives:
Map multiple vector datasets and match colors according to attributes
Custom map legend
Create a custom map
In this section, you will learn how to customize map symbols and the colors and symbols used to represent vector data in Python, using geopandas and matplotlib for mapping
First import the packages you need to use:
Import osimport matplotlib.pyplot as pltimport numpy as npfrom shapely.geometry import boximport geopandas as gpdimport earthpy as et# download data # data = et.data.get_data ('spatial-vector-lidar') os.chdir (os.path.join (et.io.HOME,' learning') 'python_data_plot')) # Import data sjer_roads_path= "data/california/madera-county-roads/tl_2013_06039_roads.shp" sjer_roads = gpd.read_file (sjer_roads_path) print (type (sjer_roads [' RTTYP'])) print (sjer_roads ['RTTYP']. Unique ()) [' M' None'S''C'] missing value replacement
You can see that there are some missing values in the road type. Since you need to draw all the road types, even those set to None, the RTTYP property None is Unknown.
Sjer_roads ['RTTYP'] .replace (np.nan, "Unknown", inplace=True) # sjer_ origins.locs [Sjer _ roads [' RTTYP'] .isnull (), 'RTTYP'] =' Unknown'print (sjer_roads ['RTTYP']. Unique ()) [' M' Unknown''S''C']
If you use geopandas.Plot () to draw data, when the column = parameter is set, geopandas will automatically select a color for the line, and you can add a legend using the legend = True parameter
Fig, ax= plt.subplots (figsize= (14Cool 6)) sjer_roads.plot (column='RTTYP', categorical=True, legend=True, ax=ax) # adjust legend position leg = ax.get_legend () leg.set_bbox_to_anchor ((1.15pr 0.5)) # Hidden border ax.set_axis_off () plt.show ()
Assign colors based on attributes
In order to draw a vector layer by attribute value, so that each road layer is shaded according to its own attribute value, so the legend represents the same symbol, which requires three steps:
Create a dictionary that associates a specific color with a specific attribute value
Loop through and apply the color to each attribute value
Finally, add a label parameter to the drawing to call ax.legend () to generate the final legend
Next, first create a dictionary to define which color you want to use to draw each road type:
# Create a dictionary where you assign each attribute value to a particular colorroadPalette = {'masked:' blue', 'slots:' green', 'Unknown'::' purple', 'Unknown':' grey'} roadPalette {'masked:' blue', 'slots:' green', 'Unknown':' grey'}
Next, loop through each attribute value and draw a line with that attribute value using the color specified in the dictionary
Fig, ax= plt.subplots (figsize= (1010)) # draw for ctype,data in sjer_roads.groupby ('RTTYP'): color= roadPalette [ctype] data.plot (color=color, ax=ax, label=ctype) ax.legend (bbox_to_anchor= (1.0,.5) Prop= {'size': 12}) ax.set (title='Madera County Roads') ax.set_axis_off () plt.show ()
Adjust line width
You can set the line width through the linewidth = property
Fig, ax= plt.subplots (figsize= (10,10)) # Loop through each group (unique attribute value) in the roads layer and assign it a colorfor ctype, data in sjer_roads.groupby ('RTTYP'): color= roadPalette [ctype] data.plot (color=color, ax=ax, label=ctype Linewidth=4) # Make all lines thicker# Add title and legend to plotax.legend () ax.set (title='Madera County Roads') ax.set_axis_off () plt.show ()
Adjust line width according to attributes
Like shading, first create a mapping relationship between line width and type, and then group to draw in a loop
# Create dictionary to map each attribute value to a line widthlineWidths = {'ctype: 1,' color= roadPalette: 1, 'color= roadPalette: 4,' Unknown':. 5} # Plot data adjusting the linewidth attributefig, ax= plt.subplots (figsize= (10,10)) ax.set_axis_off () for ctype, data in sjer_roads.groupby ('RTTYP'): color= roadPalette [ctype] data.plot (color=color, ax=ax, label=ctype) # Assign each group to a linewidth using the dictionary created above linewidth= linewidths [c type] ax.legend () ax.set (title='Madera County\ n Line width varies by TYPE Attribute Value') plt.show ()
Custom drawing legend
In the above experiment, using label=True to display the legend, the loc= parameter of ax.legend () can adjust the position of the legend. The common parameters of ax.legend () are:
Loc= (how-far-right,how-far-above)
Fontsize=, sets the legend font size
Whether frameon=, displays the legend border
LineWidths = {'ctype: 1,' RTTYP': 2, 'Unknown': 3} fig, ax= plt.subplots (figsize= (10,10)) # Loop through each attribute value and assign each # with the correct color & width specified in the dictionaryfor ctype, data in sjer_roads.groupby (' RTTYP'): color= roadPalette [ctype] label = ctype data.plot (color=color, ax=ax, linewidth=lineWidths [ctype] Label=label) ax.set (title='Madera County\ n Line width varies by TYPE Attribute Value') # Place legend in the lower right hand corner of the plotax.legend (loc='lower right', fontsize=15, frameon=True) ax.set_axis_off () plt.show ()
Observe what happens when the legend frameon property is set to False and the lineweight is adjusted. Notice that the loc = () parameter is assigned a tuple that defines the position of the legend relative to the x and y of the drawing area.
LineWidths = {'ctype: 1,' ctype: 2, 'Unknown': 3} fig, ax= plt.subplots (figsize= (10,10)) for ctype, data in sjer_roads.groupby (' RTTYP'): color= roadPalette [ctype] label = ctype data.plot (color=color, ax=ax, linewidth=lineWidths [ctype] Label=label) ax.set (title='Madera County\ n Line width varies by TYPE Attribute Value') ax.legend (loc= (1,0.5), fontsize=15, frameon=False, title= "LEGEND") ax.set_axis_off () plt.show ()
Adjust the lineweight and color at the same time
RoadPalette = {'Unknown'::' grey', 'fig: "blue",' ax: "magenta", 'Unknown': "lightgrey"} lineWidths = {' fig: 1, 'Unknown': 3} fig, ax = plt.subplots (figsize= (10,10)) for ctype Data in sjer_roads.groupby ('RTTYP'): color= roadPalette [ctype] label= ctype data.plot (color=color, ax=ax, linewidth=lineWidths [ctype], label=label) ax.set (title='Madera County Roads\ nPretty Colors') ax.legend (loc='lower right', fontsize=20, frameon=False) ax.set_axis_off () plt.show ()
Add a point layer to the map
Next, add another layer to the map to see how to create a more complex map, add SJER_plot_centroids shapefile, and represent the legend of both layers at the same time
This point layer contains three types: grass,soil,trees
# Import point layer sjer_plots_path = "data/california/neon-sjer-site/vector_data/SJER_plot_centroids.shp" sjer_plots = gpd.read_file (sjer_plots_path) sjer_plots.head (5)
.dataframe tbody tr th:only-of-type {vertical-align: middle;}
.dataframe tbody tr th {vertical-align: top;}. Dataframe thead th {text-align: right;}
Plot_ID Point northing easting plot_type geometry 0 SJER1068 center 4111567.818 255852.376 trees POINT (255852.376 4111567.818) 1 SJER112 center 4111298.971 257406.967 trees POINT (257406.967 4111298.971) 2 SJER116 center 4110819.876 256838.760 grass POINT (256838.760 4110819.876) 3 SJER117 center 4108752.026 256176.947 trees POINT (256176.947) 4 SJER120 center 4110476.079 255968.372 grass POINT (255968.372)
As you did above, create a dictionary to specify the colors associated with each graphic type
PointsPalette = {'trees':' chartreuse', 'grass':' darkgreen', 'soil':' burlywood'} lineWidths = {'ctype: .5,' fig: 2, 'Unknown':. 5} fig, ax = plt.subplots (figsize= (10,10)) for ctype, data in sjer_plots.groupby (' plot_type'): color= pointsPalette [ctype] label = ctype data.plot (color=color Ax=ax, label=label, markersize=100) ax.set (title='Study area plot locations\ n by plot type (grass, soil and trees)') ax.legend (fontsize=20, frameon=True, loc= (1, .1), title= "LEGEND") ax.set_axis_off () plt.show ()
Overlay a point layer on a road layer
Next, overlay the sketch point data on the road layer, and then create a custom legend that contains lines and points
Note: in this example, the projection information of the two layers must match
# Reproject the data# data projection sjer_roads_utm = sjer_roads.to_crs (sjer_plots.crs) fig, ax= plt.subplots (figsize= (10,10)) # Point layer drawing for ctype, data in sjer_plots.groupby ('plot_type'): color= pointsPalette [ctype] label= ctype # label parameters are important for legend generation data.plot (color=color, ax=ax, label=label) Markersize=100) # Road layer drawing for ctype, data in sjer_roads_utm.groupby ('RTTYP'): color= roadPalette [ctype] label= ctype data.plot (color=color, ax=ax, linewidth=lineWidths [ctype], label=label) ax.set (title='Study area plot locations\ n by plot type (grass, soil and trees)') ax.legend (fontsize=15, frameon=False Loc= ('lower right'), title= "LEGEND") ax.set_axis_off () plt.show () the above is how to draw vector data in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.