Python Forum
[Solved] Matplotlib - Tricontour: how to set colorbar range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Solved] Matplotlib - Tricontour: how to set colorbar range (/thread-35770.html)



[Solved] Matplotlib - Tricontour: how to set colorbar range - ju21878436312 - Dec-13-2021

I would like to set the colorbar range, when plotting my data, using tricontour.

What I have tried:
vmin=0.13, vmax=0.4
to enforce min and max, but it did not show any difference.

plt.clim(0.13,0.4)
which creates an error:
RuntimeError: You must first define an image, e.g., with imshow
Here is my code, you can find "data.csv" as attachement to this message.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.tri as tri

# load data
df = pd.read_csv("data.csv", sep=";")

# specify x, y, z
x = df.iloc[:, 0]
y = df.iloc[:, 1]
z = df.iloc[:, 2]

# create plot
fig, ax = plt.subplots(nrows=1)

# contoure
ax.tricontour(x, y, z, levels=14, linewidths=0.5, colors='k',vmin=0.13, vmax=0.4) 
# filling
cntr2 = ax.tricontourf(x, y, z, levels=14, cmap="RdBu_r",vmin=0.13, vmax=0.4) 
# colorbar
fig.colorbar(cntr2, ax=ax)
# dots
ax.plot(x, y, 'ko', ms=3) 

# Equal aspect ratio 
plt.gca().set_aspect('equal', adjustable='box')
# plt.clim(0.13,0.4) 
plt.show()



RE: [Solved] Matplotlib - Tricontour: how to set colorbar range - ju21878436312 - Dec-13-2021

I found the solution by myself: via levels you can specify the exact vector for the colorbar range.

levels =[0.1,0.2,0.3,0.4]