zgfu1985
zgfu1985

Reputation: 131

How to get the pure color filling for 'tripcolor' in matplotlib

The class 'tripcolor' in matplotlib gives a very powerful tool to create a contour-style plot for those 2D-triangles just like:

plt.tripcolor(x,y,triangles,vars,cmap='PuOr')

Though there are many types of defined colormaps for usage, but now I cannot find a proper method just for pure color filling for these triangles. Does anybody know how to do it? Thanks for telling me.

Upvotes: 2

Views: 1379

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339290

It feels rather strange, but it seems the easiest solution to have a tripcolor plot filled with a single color is to create a colormap which only has a single color in it and supply it to the tripcolor.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors

xy = np.asarray([ [-0.01, 0.9], [-0.1, 0.8], [-0.05, 0.6], [-0.08, 0.9]])
triangles = np.asarray([[3, 2, 0]  , [3, 1, 2],   [ 0, 2, 1] , [0, 1, 2]])

# generate some array of length same as xy
c = np.ones(len(xy))
# create a colormap with a single color
cmap = matplotlib.colors.ListedColormap("limegreen")
# tripcolorplot with a single color filling:
plt.tripcolor(xy[:,0],xy[:,1],triangles, c, edgecolor="k", lw=2, cmap=cmap)
plt.show()

enter image description here

Upvotes: 1

Related Questions