🔍 Extract Field Names Containing 'type' (Integer Fields Without Domain) from GDB Using ArcPy

  ⚙️ How the Script Works 🗂️ Geodatabase Setup The script starts by pointing to a target File Geodatabase (.gdb) and initializing a CSV ...

Saturday, April 19, 2025

Post #4: Plotting and Styling GIS Data with Python

 

Title: Plot Beautiful Maps with Python: GeoPandas + Matplotlib Styling Tips


📍Introduction

Once you've loaded, joined, or filtered your GIS data, the next step is to visualize it. A good map tells the story at a glance—and Python gives you full control over how your map looks.

In this post, we'll cover:

  • Basic plotting with GeoPandas

  • Customizing colors, borders, legends, and labels

  • Exporting high-quality images for reports

Let’s turn spatial data into beautiful, readable maps.


🧰 Step 1: Load Your Layer

We’ll use a polygon and point layer example—like districts and wells.

python
import geopandas as gpd import matplotlib.pyplot as plt districts = gpd.read_file("data/districts.shp") wells = gpd.read_file("data/water_wells.shp") # Ensure CRS is the same districts = districts.to_crs("EPSG:4326") wells = wells.to_crs("EPSG:4326")

🗺️ Step 2: Basic Map Plot

python
districts.plot()

Now let’s customize it:

python
fig, ax = plt.subplots(figsize=(12, 10)) districts.plot(ax=ax, color='lightyellow', edgecolor='black') wells.plot(ax=ax, color='blue', markersize=10) ax.set_title("Water Wells in Districts", fontsize=16) plt.axis("off") plt.tight_layout() plt.show()

🎨 Step 3: Add Styling by Attribute

Color districts by population:

python
districts.plot(column='population', cmap='OrRd', legend=True, edgecolor='grey')

You can use other colormaps like "YlGnBu", "viridis", "plasma", "coolwarm", etc.


📍 Step 4: Add Labels to Features

Add district names:

python
for idx, row in districts.iterrows(): plt.text(row.geometry.centroid.x, row.geometry.centroid.y, row["district_name"], fontsize=8, ha='center', color='darkred')

💡 Use .centroid for polygons. For points, just use row.geometry.x, row.geometry.y.


🖼️ Step 5: Export the Map as Image

Save your map as PNG or PDF:

python
fig.savefig("output/districts_wells_map.png", dpi=300) fig.savefig("output/districts_wells_map.pdf")

💡 Bonus Styling Tips

  • Use alpha=0.5 to control transparency

  • Try markersize or linewidth to emphasize layers

  • Overlay multiple GeoDataFrames on the same ax for composite maps

  • Annotate features using ax.annotate()


🧠 Why Use Python for Mapping?

  • Generate maps automatically from any dataset

  • Create hundreds of maps in a loop (e.g., one per district)

  • Customize layouts for reports, posters, or dashboards

  • Integrate maps directly into data pipelines


🎯 Conclusion

With a few lines of code, you can build custom maps that are informative, beautiful, and ready for publication. No need to open a GIS interface every time—you now have a Python-powered map studio at your fingertips.


📌 Next Up:

➡️ Post 5: Automate Map Creation from Shapefiles in Bulk

No comments:

Post a Comment