python - Matplotlib:等高线水平作为颜色条中的线

标签 python matplotlib plot

我正在使用 Matplotlib 绘制一些二维数据作为 pcolor(),然后将其与 contour() 叠加。

enter image description here

当我使用 colorbar() 时,我得到以下颜色条之一:

colorbars for <code>contour()</code> or <code>pcolor()</code>

如何使等高线级别的水平线(左)也显示在彩色条(右)中?

最佳答案

根据您修改后的问题,我明白您的意思。这仍然可以使用 add_lines 来完成。此函数将非填充等高线图中的线条添加到颜色条中。可以找到文档 here .

因此,通过首先根据您的 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()

enter image description here

关于python - Matplotlib:等高线水平作为颜色条中的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26018258/

相关文章:

r - 如何用特定颜色表示等高线图(fill.contour)中的最佳区域?

python - django 对象保存在开发中的 sqlite 和生产中的 mysql 中有所不同

python - 如何在不使用信用卡或没有钱的情况下在 Heroku 中安排 Django 后台任务?

python - 使用 Excel 工作表中的数据在 Python 中绘制图表

python - 北极/日界线 Cartopy 奇点中的 Matplotlib Contour/Contourf

pandas - 如何从 Pandas 系列绘制条形图?

python - 初始化和存储 matplotlib gridspec 布局

python - MPI4Py 分散 sendbuf 参数类型?

python - (x, y, z) 列表值的轮廓

python - 使用 Pandas/matplotlib 绘制日均值时更改日期时间刻度的格式