I'm new to GIS and QGIS. I've looked through several of the related questions including here and here, but can't figure out a simple way to automate creating choropleths for each attribute in a shapefile using QGIS but strictly from Python. I figured this was a relatively common use case.

I gave up after about four or so hours of trying to figure it out and wrote this simple code that produces the ~300 pngs that I need:

import geopandas as gpd
import matplotlib.pyplot as plt

sp = gpd.read_file(<shapefile filename goes here>)
column_list = sp.columns.values
for i in column_list: 
    sp.plot(column = i, cmap='Blues', alpha=1, legend=True, linewidth=0)
    plt.savefig(i +'.png', dpi=600,bbox_inches='tight')

Again this works fine for what I needed, but is there an implementation in QGIS? I'd like to get better with this software/package.


Here is one method of how to do this:

  1. Define a list variable containing the field names that you want to create a choropleth on
  2. Loop over step 1 list and create an in-memory layer with the field name and populate it (attributes and features) from source layer and add it to canvas (see code example: Duplicating layer in memory using pyqgis?)
  3. Create the categorized renderer for the in-memory layer (see code example: Apply symbol to each feature (Categorized symbol))
  4. Continue looping steps 2-3
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.