0

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.

Javier
  • 493
  • 3
  • 15
  • 1
    The same error occurred when I was answering your one previous question. It seems that `c=tmaxvalores` is the cause. When I excluded it, the map showed up. cmap works with a range from 0 to 1, but I think the reason is that the color values exceed that range. This is just my guess. – r-beginners Apr 07 '21 at 02:39
  • Yeah, the issue was the ranges in rvbtmax. I can't exceed my maximum value. Thanks for the advice. – Javier Apr 07 '21 at 04:19

0 Answers0