Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib
#1
Hi everyone,


I've created a code to run a 2D mapping using matplotlib from a .csv file.
I'd like 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?
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') 
Reply
#2
Not sure which column in your dataframe is "red", but something like
df["red"] = df["red"]*0.8
should work for the column
julienhofmann likes this post
Reply
#3
Thank you for your response!
I just tried it but it doesn't make what I want.

Bassically, I would like to not put any color for every values above 0.8 times the maximum value (ie. 488).
Hence, the ''maximum'' color (ie. red) would correspond to 488 and not to 610 as currently.
Reply
#4
If I understand, you want to clip it at 488 rather than scale to 80%. Correct?

If so,
df['red'] = df['red'].apply(lambda x: x if x<488 else 488)
Reply
#5
Yes right
488 is 80% of my maximum value, which corresponds to the same.

I've also tried to include vmin and vmax in contourf(). It works but the values above 80% of the maximum value still remain red which makes the cartography not really clear.

I think I should put it in black, or white, every values above 80% of the maximum value.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020