1

I am trying to interpolate & plot some irregularly spaced data as a contour plot. I want to manually set the contour levels to some arbitrary values. The problem I am running into is the colorbar. I want each level to have its own color, but when I manually pass an arraylike of my desired contour levels to tricontourf, the colorbar sets all but one level to the same color, and the final level to the next color in sequence.

The data used to generate the plot

My code:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('testdata.txt')

x = data[:,0]
y = data[:,1]
z = data[:,2]

levels = [0, 1, 2, 3, 4, np.max(z)]

plt.figure()

cntr1 = plt.tricontourf(x,y,z, levels=levels, cmap="viridis_r")
plt.colorbar(cntr1, ticks=levels)

plt.show()

Resulting plot with first 4 levels all the same color (I want them to be different colors):

result

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

0 Answers0