Notification texts go here Contact Us Buy Now!

Cannot Rotate Legend Horizontally in GeoPandas

I embarked on creating a Minimum Working Example (MWE) script, and in the process, I stumbled upon the root of the issue. The COUNTYFP column had an object data type, causing matplotlib to generate discrete colors. Strangely, the orientation parameter only seems to function for colorbars. Once I converted COUNTYFP to numeric data, the problem was resolved.

Here's the code snippet that demonstrates the fix:

import geopandas as gpd
import matplotlib.pyplot as plt

# Read shapefile
data = gpd.read_file("path/to/shapefile.shp")

# Convert COUNTYFP column to numeric
data['COUNTYFP'] = data['COUNTYFP'].astype(int)

# Plot choropleth map
data.plot(column='COUNTYFP', legend=True, legend_kwds={'orientation': 'horizontal'})

plt.show()

In summary, the culprit was the object data type of the COUNTYFP column. By converting it to numeric, matplotlib was able to generate a continuous colorbar, and the orientation parameter functioned as expected.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.