Python Matplotlib add Colorbar

I have a problem using MatlobLib with "Custom" Shapes from a shader. Importing and viewing pasted faces works fine, but I can’t place a color chart on my shape.

I have already tried several methods from the tutorial, but I'm sure there is a reasonable solution to this problem.

Maybe someone can help me, my current code is below:

from formencode.national import pycountry import itertools from matplotlib import cm, pyplot from matplotlib import from mpl_toolkits.basemap import Basemap from numpy.dual import norm import cartopy.crs as ccrs import cartopy.io.shapereader as shpreader import matplotlib as mpl import matplotlib.colors as colors import matplotlib.mlab as mlab import matplotlib.pyplot as plt import numpy as np def draw_map_for_non_normalized_data_with_alpha2_counrty_description(data, title=None): m = Basemap() ax = plt.axes(projection=ccrs.PlateCarree()) list = [] sum = 0 for key in data: sum += data[key] for key in data.keys(): new_val = (data[key]+0.00)/sum list.append(new_val) data[key] = new_val #=========================================================================== # print str(min(list)) # print str(max(list)) #=========================================================================== cmap = mpl.cm.cool colors = matplotlib.colors.Normalize(min(list)+0.0, max(list)+0.0) labels = [] features = [] for country in shpreader.Reader(shapename).records(): a3_code = country.attributes["gu_a3"] try : a2_code = pycountry.countries.get(alpha3=a3_code).alpha2 except: a2_code = "" if a2_code in data: val = data[a2_code] color = cm.jet(norm(val)) print str(val) + " value for color: " + str(color) labels.append(country.attributes['name_long']) feat = ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=color, label=country.attributes['name_long']) features.append(feat) #ax.legend(features, labels, loc='upper right') #=========================================================================== # fig = pyplot.figure(figsize=(8,3)) # ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15]) #=========================================================================== #cbar = m.colorbar(location='bottom') cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,norm=colors,orientation='horizontal') cb1.set_label('foo') m.drawcoastlines() m.drawcountries() if title: plt.title(title) plt.show() 

as you can see inside the code, I already tried several methods, but none of them worked for me.

Maybe someone has a β€œclue” for me.

thanks for the help,

respectfully

+5
source share
1 answer

As mentioned in the comments above, I would think twice about mixing Basemap and Cartopy , is there a specific reason for this? Both of them basically do the same, expanding Matplotlib with geographic capabilities. Both are valid for use, they both have their own pros and con.

In your example, you have the Basemap m axes, the Cartopy axes, and you use the Pylab interface using plt. which works with currently active axes. Perhaps this is theoretically possible, but to me it seems error prone.

I cannot change your example to make it work, because the data is missing and your code is invalid Python, for example, the indent for the function is incorrect. But here is an example with potatoes showing how you can build a Shapefile and use the same cmap/norm combination to add a color panel to the axes.

One difference of your code is that you provide the axes containing the map with the ColorbarBase functions, these must be separate axes specifically for the color bar.

 import cartopy.crs as ccrs import matplotlib.pyplot as plt import matplotlib as mpl import cartopy.io.shapereader as shpreader fig, ax = plt.subplots(figsize=(12,6), subplot_kw={'projection': ccrs.PlateCarree()}) norm = mpl.colors.Normalize(vmin=0, vmax=1000000) cmap = plt.cm.RdYlBu_r for n, country in enumerate(shpreader.Reader(r'D:\ne_50m_admin_0_countries_lakes.shp').records()): ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=cmap(norm(country.attributes['gdp_md_est'])), label=country.attributes['name']) ax.set_title('gdp_md_est') cax = fig.add_axes([0.95, 0.2, 0.02, 0.6]) cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm, spacing='proportional') cb.set_label('gdp_md_est') 

enter image description here

+12
source

Source: https://habr.com/ru/post/1201151/


All Articles