I'm using the function make_colormap to make my own colors and colorbar in a map. Source: Create own colormap using matplotlib and plot color scale
This is the function:
import matplotlib.colors as mcolors
def make_colormap(seq):
"""Return a LinearSegmentedColormap
seq: a sequence of floats and RGB-tuples. The floats should be increasing
and in the interval (0,1).
"""
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, b1, b2])
return mcolors.LinearSegmentedColormap('CustomMap', cdict)
c = mcolors.ColorConverter().to_rgb
Also i'm defining my range of values with the minimum and maximum value:
vmintmax = min(tmax[['ENE','FEB','MAR','ABR','MAY','JUN','JUL','AGO','SEP','OCT','NOV','DIC']].min()) # the overall minimum
vmaxtmax = max(tmax[['ENE','FEB','MAR','ABR','MAY','JUN','JUL','AGO','SEP','OCT','NOV','DIC']].max()) # the overall maximum
normtmax = plt.Normalize(vmintmax, vmaxtmax) # function that maps the range of tmax to the range [0,1]
And i'm defining my color values:
rvbtmax = make_colormap([c('lime'), c('lime'), normtmax(11), c('forestgreen'), c('forestgreen'),
normtmax(13), c('lightgreen'), c('lightgreen'), normtmax(15), c('lawngreen'),
c('lawngreen'), normtmax(17),c('greenyellow'), c('greenyellow'),normtmax(19),c('yellow'), c('yellow'),
normtmax(21),c('khaki'), c('khaki'),normtmax(23),c('gold'), c('gold'),normtmax(25),
c('goldenrod'), c('goldenrod'),normtmax(27),c('orange'), c('orange'),normtmax(29),c('orangered'), c('orangered'),normtmax(31),
c('red'), c('red'),normtmax(33),c('firebrick'), c('firebrick'),normtmax(35),
c('darkred'), c('darkred')])
Finally i'm plotting my map here:
for mes in ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio',
'Agosto','Septiembre','Octubre','Noviembre','Diciembre']:
data = tmax[['CODIGO', 'LONGITUD', 'LATITUD', mes]]
lons, lats= np.array(data['LONGITUD']), np.array(data['LATITUD'])
tmaxvalores=np.array(data[mes]).astype(int)
fig = plt.figure('map', figsize=(7,7), dpi=200)
ax = fig.add_axes([0.1, 0.12, 0.80, 0.75], projection=ccrs.PlateCarree())
plt.title('\nNormales Climáticas Mensuales de Temperatura Máxima\nMes de'+f' {mes}'+'\nPeríodo 1981-2010\n')
plt.xlabel('LONGITUD')
plt.ylabel('LATITUD')
ax.outline_patch.set_linewidth(0.3)
l = NaturalEarthFeature(category='cultural', name='admin_0_countries', scale='50m', facecolor='none')
ax.add_feature(l, edgecolor='black', linewidth=0.1)
img = ax.scatter(lons, lats, s=7, c=tmaxvalores, cmap=rvbtmax, norm=normtmax,
marker='o', transform=ccrs.PlateCarree())
But when im plotting the map i get this error: ValueError: data mapping points must have x in increasing order
I have no idea why i get this error. With similar df's and same code i don't get this error. Would you mind to help me?
Thanks in advance.