如何用不同颜色的等高线图覆盖控制图?

2024-05-14 10:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

(我问了the same in MATLAB before

我想用一个seismic-cmapedcontourf-plot(或pcolor)覆盖一个gray比例contour-绘图,但是当我添加后者时,它也会改变之前的颜色映射。如何解决这个问题?在


Tags: thein绘图plot颜色比例contoursame
1条回答
网友
1楼 · 发布于 2024-05-14 10:00:13

这个答案几乎完全来自contour demo example

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab # for setting up the data
import matplotlib.pyplot as plt

# set up example data:
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

levels = 10

# plot the filled contour
# using a colormap (jet)
CF = plt.contourf(Z, levels,
                  extent=(-3,3,-2,2),cmap=cm.jet)

# plot the contour lines
# using gray scale
CL = plt.contour(Z, levels,
                 linewidths=2,
                 extent=(-3,3,-2,2),cmap=cm.gray)

# plot color bars for both contours (filled and lines)
CB = plt.colorbar(CL, extend='both')
CBI = plt.colorbar(CF, orientation='horizontal')

# Plotting the second colorbar makes 
# the original colorbar look a bit out of place,
# so let's improve its position.

l,b,w,h = plt.gca().get_position().bounds
ll,bb,ww,hh = CB.ax.get_position().bounds
CB.ax.set_position([ll, b, ww, h])

plt.show()

最后你会有这样的情节:

two color scales

相关问题 更多 >

    热门问题