简体   繁体   English

Matplotlib:contourlevels作为colorbar中的行

[英]Matplotlib: contourlevels as lines in colorbar

I'm plotting some 2D data with Matplotlib once as pcolor() , then superposing this with contour() . 我用Matplotlib绘制一些2D数据作为pcolor() ,然后用contour()重叠它。

在此输入图像描述

When I use colorbar() I get either one or the other of the following colorbars: 当我使用colorbar()我得到以下颜色栏中的一个或另一个:

<code> contour()</ code>或<code> pcolor()</ code>的颜色条

How do I make the horizontal lines for the contour levels(left) also show in the colored bar(right)? 如何使轮廓水平(左)的水平线也显示在彩色条(右)中?

Based from your revised question I get what you mean. 根据您修改后的问题,我明白您的意思。 This can still be done using add_lines . 这仍然可以使用add_lines完成。 This function adds the lines from a non-filled contour plot to a colorbar. 此功能将非填充等高线图中的线条添加到颜色条。 The documentation can be found here . 文档可以在这里找到。

So, by first defining the colorbar based on your pcolor plot you can later add the lines from contour to that colorbar: 因此,通过首先根据您的pcolor图定义颜色条,您可以稍后将contour线添加到该颜色条:

import numpy
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

#Generate data
delta = 0.025

x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)

X, Y = numpy.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)

#Plot
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

PC = ax1.pcolor(X, Y, Z)
CF = ax1.contour(X, Y, Z, 50, colors = "black")

cbar = plt.colorbar(PC)
cbar.add_lines(CF)

plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM