## NumPy – 84 – grafici geografici con dati Basemap – 2

Continuo da qui, copio qui.

Tipi di mappe
Earlier we saw the bluemarble() and shadedrelief() methods for projecting global images on the map, as well as the drawparallels() and drawmeridians() methods for drawing lines of constant latitude and longitude. The Basemap package contains a range of useful functions for drawing borders of physical features like continents, oceans, lakes, and rivers, as well as political boundaries such as countries and US states and counties. The following are some of the available drawing functions that you may wish to explore using IPython’s help features:

confini fisici e bacini d’acqua

• drawcoastlines(): Draw continental coast lines
• drawlsmask(): Draw a mask between the land and sea, for use with projecting images on one or the other
• drawmapboundary(): Draw the map boundary, including the fill color for oceans.
• drawrivers(): Draw rivers on the map
• fillcontinents(): Fill the continents with a given color; optionally fill lakes with another color

confini politici

• drawcountries(): Draw country boundaries
• drawstates(): Draw US state boundaries
• drawcounties(): Draw US county boundaries
• Caratteristiche delle mappe
• drawgreatcircle(): Draw a great circle between two points
• drawparallels(): Draw lines of constant latitude
• drawmeridians(): Draw lines of constant longitude
• drawmapscale(): Draw a linear scale on the map

immagini globali

• bluemarble(): Project NASA’s blue marble image onto the map
• shadedrelief(): Project a shaded relief image onto the map
• etopo(): Draw an etopo relief image onto the map
• warpimage(): Project a user-provided image onto the map

For the boundary-based features, you must set the desired resolution when creating a Basemap image. The resolution argument of the Basemap class sets the level of detail in boundaries, either 'c' (crude), 'l' (low), 'i' (intermediate), 'h' (high), 'f' (full), or None if no boundaries will be used. This choice is important: setting high-resolution boundaries on a global map, for example, can be very slow.

Here’s an example of drawing land/sea boundaries, and the effect of the resolution parameter. We’ll create both a low- and high-resolution map of Scotland’s beautiful Isle of Skye. It’s located at 57.3°N, 6.2°W, and a map of 90,000 × 120,000 kilometers shows it well:

Notice that the low-resolution coastlines are not suitable for this level of zoom, while high-resolution works just fine. The low level would work just fine for a global view, however, and would be much faster than loading the high-resolution border data for the entire globe! It might require some experimentation to find the correct resolution parameter for a given view: the best route is to start with a fast, low-resolution plot and increase the resolution as needed.

Disegnare dati sulle mappe
Perhaps the most useful piece of the Basemap toolkit is the ability to over-plot a variety of data onto a map background. For simple plotting and text, any plt function works on the map; you can use the Basemap instance to project latitude and longitude coordinates to (x, y) coordinates for plotting with plt, as we saw earlier in the Seattle example.

In addition to this, there are many map-specific functions available as methods of the Basemap instance. These work very similarly to their standard Matplotlib counterparts, but have an additional Boolean argument latlon, which if set to True allows you to pass raw latitudes and longitudes to the method, rather than projected (x, y) coordinates.

Some of these map-specific methods are:

• contour()/contourf() : Draw contour lines or filled contours
• imshow(): Draw an image
• pcolor()/pcolormesh(): Draw a pseudocolor plot for irregular/regular meshes
• plot(): Draw lines and/or markers.
• scatter(): Draw points with markers.
• quiver(): Draw vectors.
• barbs(): Draw wind barbs.
• drawgreatcircle(): Draw a great circle.

We’ll see some examples of a few of these as we continue. For more information on these functions, including several example plots, see the online Basemap documentation.

Esempio: le città della California
Recall that in Customizing Plot Legends [qui], we demonstrated the use of size and color in a scatter plot to convey information about the location, size, and population of California cities. Here, we’ll create this plot again, but using Basemap to put the data in context.

Serve la directory data contenente il DB csv già usato per il post indicato.
Siccome lo script è decisamente lungo non metto lo screenshot ma il codice (cal-cities.py).

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

import pandas as pd

# Extract the data we're interested in
lat = cities['latd'].values
lon = cities['longd'].values
population = cities['population_total'].values
area = cities['area_total_km2'].values

# Next, we set up the map projection, scatter the data,
#       and then create a colorbar and legend:

# 1. Draw the map background
fig = plt.figure(figsize=(8, 8))
m = Basemap(projection='lcc', resolution='h',
lat_0=37.5, lon_0=-119,
width=1E6, height=1.2E6)
m.drawcoastlines(color='gray')
m.drawcountries(color='gray')
m.drawstates(color='gray')

# 2. scatter city data, with color reflecting population
# and size reflecting area
m.scatter(lon, lat, latlon=True,
c=np.log10(population), s=area,
cmap='Reds', alpha=0.5)

# 3. create colorbar and legend
plt.colorbar(label=r'$\log_{10}({\rm population})$')
plt.clim(3, 7)

# make legend with dummy points
for a in [100, 300, 500]:
plt.scatter([], [], c='k', alpha=0.5, s=a,
label=str(a) + ' km$^2$')
plt.legend(scatterpoints=1, frameon=False,
labelspacing=1, loc='lower left');

fig.savefig('np813.png')

Al solito alcune funzioni sono deprecate, se si dovesse usare ci sarebbero aggiornamenti da considerare, il mondo avanza, non si ferma mai 😯:

This shows us roughly where larger populations of people have settled in California: they are clustered near the coast in the Los Angeles and San Francisco areas, stretched along the highways in the flat central valley, and avoiding almost completely the mountainous regions along the borders of the state.

A questo punto Jake propone un altro esempio, Example: Surface Temperature Data ma i dati richiesti non sono più disponibili; ho tentato di recuperare i nuovi dati sul sito della NASA, qui, ma non sono riuscito a riprodurre l’esempio. Che peraltro è qualcosa diverso dai miei interessi informatici. Il riscaldamento globale invece è un grosso problema; per tutti.

Annunci