Python Forum

Full Version: [Solved] Matplotlib - Tricontour: how to set colorbar range
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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]