Matplotlib scale

Thomas Jollans tjol at tjol.eu
Mon Apr 5 07:53:35 EDT 2021


On 04/04/2021 20:57, Julien Hofmann wrote:
> Hi everyone,
>
>
> I've created a code to run a 2D mapping using matplotlib from a .csv file.
> I've tried to set the maximum color (red) of the scale as 80% of the maximum value and not as the maximum value of my .csv file.
> Does someone know how to modify that?

Most (or all?) matplotlib functions and methods that take a cmap 
argument also take vmin and vmax arguments to specify the maximum and 
minimum values to assign colours to.

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html

So adding vmax=0.8*np.max(z) to your contourf() call should do the trick.

A couple of quick data visualisation tips:

1. if your colour map doesn't cover the full range of data, your colour 
bar should indicate this. Call fig.colorbar(...., extend='max').

2. 'jet' is a *terrible* colour map and you should *never* use it. It 
distorts your data, making people see patterns that aren't there, and is 
all but useless in black & white printouts or to the colour-blind.

This seminal talk from 2015 explains why the default Matplotlib colour 
maps are what they are: https://www.youtube.com/watch?v=xAoljeRJ3lU


-- Thomas


> I've tried different solution but it doesn't work.
>
> Thanks
>
> import os
> import matplotlib.pyplot as plt
> import pandas as pd
> import numpy as np
> from matplotlib import colorbar, colors
> import matplotlib.tri as tri
>   
> #os.chdir("C:/Users/Julien Hofmann/Desktop/Nano-indentation")
> data = pd.read_csv("Cartographie.csv",sep=';')
>   
> nb_lignes=21
> nb_colonnes=27
>   
>   
> fig = plt.figure(figsize=(15,12))
> ax = plt.subplot(1,1,1)
> x=np.linspace(0,(data["x"][len(data["x"])-1]-data["x"][0])*1000,nb_colonnes)
> y=np.linspace(0,(data["y"][len(data["y"])-1]-data["y"][0])*1000,nb_lignes)
> X,Y=np.meshgrid(x,y)
>   
>   
> z=np.array(data["Durete"])
> triang = tri.Triangulation(data["x"], data["y"])
> interpolator = tri.LinearTriInterpolator(triang, z)
> Xi, Yi = np.meshgrid(x, y)
> zi = interpolator(Xi, Yi)
> cntr1 = ax.contourf(x, y, z.reshape(nb_lignes,nb_colonnes), levels=150, cmap="jet")
> cbar = fig.colorbar(cntr1, ax=ax)
> ax.axis('on')


-- 
Dr. Thomas Jollans

☎ +49 6201 8759879
✉ tjol at tjol.eu



More information about the Python-list mailing list