2016-07-20 2 views
0

Voici un échantillon des données que j'essaie de visualiserComment puis-je définir une couleur spéciale pour `Nan`s dans mon intrigue?

Prince Edward Island 2.333 
Manitoba    2.529 
Alberta     2.6444 
British Columbia  2.7902 
Saskatchewan   2.9205 
Ontario     3.465 
New Brunswick   3.63175 
Newfoundland and Labrador 3.647 
Nova Scotia    4.25333333333 
Quebec     4.82614285714 
Nunavut     NaN 
Yukon     NaN 
Northwest Territories NaN 

Je veux visualiser les données en coloriant chaque province en fonction du nombre auquel il est associé. Quand je fais cela, les Nan sont colorés comme la valeur minimale de la palette de couleurs. Y at-il un moyen facile de cartographier Nan en blanc?

Voici mon code:

plt.figure(figsize=(15,15)) 


vmin, vmax = canada.Partying.min(), canada.Partying.max() 

ax = canada.plot(column='Partying', cmap='viridis', vmin=vmin, vmax=vmax) 

# add colorbar 
fig = ax.get_figure() 
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8]) 
sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=vmin, vmax=vmax)) 
# fake up the array of the scalar mappable. Urgh... 
sm._A = [] 
fig.colorbar(sm, cax=cax) 
plt.savefig('Canada.pdf') 
+1

Comme 'canada.fillna (0.25)' – ysearka

+0

Filtrer nan valeurs: 'canada = canada.dropna (seuil = 1)'. – Serenity

+0

@ysearka Je veux que les provinces montrent en blanc. En les remplissant avec une valeur, ils seront mis en correspondance avec une couleur non blanche. –

Répondre

0

Vous pouvez combiner deux couches:

## import statements 
import geopandas as gpd 
import numpy as np 
import matplotlib.pyplot as plt 

## load the Natural Earth data set 
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) 

## add a column with NaNs 
## here we set all countries with a population > 10e7 to nan 
world["pop_est_NAN"] = world.pop_est.apply(lambda x: x if x <10e7 else np.nan) 

## first layer, all geometries included 
ax = world.plot(color="grey") 

## second layer, NaN geometries excluded 
## we skip the entries with NaNs by calling .dropna() on the dataframe 
## we reference the first layer by ax=ax 
## we specify the values we want to plot (column="pop_est") 
world.dropna().plot(ax=ax, column="pop_est") 

## add title 
ax.set_title("Countries with a population > 10e7 (= missing values) \nare plotted in grey"); 

## save fig 
plt.savefig("geopandas_nan_plotting.png", dpi=200) 

geopandas_nan_plotting

Jetez un oeil à la documentation geopandas pour une méthode alternative à l'aide matplotlib objets. Qu'en est-il du remplacement des valeurs de NaN par une certaine valeur?