Geavanceerd Plotten met Matplotlib en Seaborn
Veeg om het menu te tonen
Bij het werken met ruimtelijke data is effectieve visualisatie essentieel om patronen te ontdekken en inzichten te communiceren. De Python-bibliotheken matplotlib en seaborn bieden krachtige hulpmiddelen om ruimtelijke plots te verbeteren, waarmee je kleuren kunt aanpassen, informatieve legenda's kunt toevoegen, beschrijvende titels kunt instellen en meerdere datalagen kunt combineren. Door deze technieken te beheersen, kun je duidelijke, overtuigende kaarten en visualisaties maken die belangrijke ruimtelijke relaties benadrukken.
Het aanpassen van ruimtelijke plots begint vaak met het bepalen van het uiterlijk van je data. Je kunt kleuren specificeren om verschillende categorieën of waarden weer te geven, de transparantie aanpassen om overlappende kenmerken zichtbaar te maken, en legenda's gebruiken om je visuele coderingen toe te lichten. Het toevoegen van titels en as-labels helpt kijkers je plots snel te interpreteren. Het combineren van meerdere lagen—zoals wegen bovenop administratieve grenzen—kan extra context bieden voor je ruimtelijke analyse.
12345678910111213141516171819202122232425262728293031323334import geopandas as gpd import matplotlib.pyplot as plt url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" world = gpd.read_file(url) # Create a plot with customizations fig, ax = plt.subplots(figsize=(12, 8)) # Plot countries, coloring by continent world.plot(column='CONTINENT', cmap='Set2', edgecolor='black', linewidth=0.5, legend=True, ax=ax, alpha=0.8) # Overlay country boundaries with a thicker line for emphasis world.boundary.plot(ax=ax, color='gray', linewidth=1.5) # Add a title and axis labels ax.set_title('World Countries by Continent', fontsize=18) ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') # Customize legend position so it doesn't overlap the map leg = ax.get_legend() if leg: leg.set_bbox_to_anchor((1.15, 0.5)) plt.tight_layout() plt.show()
De kolomnaam in de officiële dataset is CONTINENT
Naast kaartgebaseerde visualisaties kun je ook de verdeling van ruimtelijke attributen — zoals oppervlakte, bevolking of dichtheid — onderzoeken met statistische grafieken. Seaborn is hiervoor bijzonder geschikt en maakt het mogelijk om histogrammen, spreidingsdiagrammen en meer te maken, allemaal met aantrekkelijke standaardstijlen en eenvoudige aanpassingsmogelijkheden. Door seaborn te combineren met ruimtelijke data kun je snel trends en uitschieters in je datasets identificeren, wat diepgaandere ruimtelijke analyses ondersteunt.
1234567891011121314151617181920212223242526272829303132import geopandas as gpd import seaborn as sns import matplotlib.pyplot as plt # Load sample data directly from the official URL url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" world = gpd.read_file(url) # The official dataset doesn't have a pre-made 'area' column, so we calculate it. # Note: This calculates area in decimal degrees based on the WGS84 projection. world['area'] = world.geometry.area # Plot a histogram of country areas plt.figure(figsize=(10, 6)) # 'area' is our new custom column, kde=True adds the density curve sns.histplot(world['area'], bins=30, color='skyblue', kde=True) plt.title('Distribution of Country Areas') plt.xlabel('Area (sq. degrees)') plt.ylabel('Number of Countries') plt.tight_layout() plt.show() # Plot a scatter plot of population vs. area plt.figure(figsize=(10, 6)) # Adjusted column names to match the official dataset: 'POP_EST' and 'CONTINENT' sns.scatterplot(x='area', y='POP_EST', data=world, hue='CONTINENT', palette='Set2') plt.title('Country Population vs. Area by Continent') plt.xlabel('Area (sq. degrees)') plt.ylabel('Estimated Population') plt.legend(title='Continent', bbox_to_anchor=(1.05, 1), loc='upper left') plt.tight_layout() plt.show()
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.