本文介绍了Matplotlib:颜色栏的高度与打印的高度相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一些2D数据,如图所示.轴纵横比应相等,并且轴范围应不同.

I'm plotting some 2D data as shown. The axes aspect should be equal and the axes range should differ.

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

#Generate data
delta = 0.025

x = numpy.arange(-5.0, 5.0, delta)
y = numpy.arange(-5.0, 5.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,  aspect='equal')

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

plt.xlim(-4.0, 4.0)
plt.ylim(-2.0, 2.0)

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

plt.show()

我如何使软木塞与绘制的数据具有相同的高度?

How can I make the colobar has the same height as the plotted data?

推荐答案

您可以使用 make_axes_locatable :

import numpy
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from mpl_toolkits.axes_grid1 import make_axes_locatable

#Generate data
delta = 0.025

x = numpy.arange(-5.0, 5.0, delta)
y = numpy.arange(-5.0, 5.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,  aspect='equal')

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

plt.xlim(-4.0, 4.0)
plt.ylim(-2.0, 2.0)

divider = make_axes_locatable(ax1)
cax1 = divider.append_axes("right", size="5%", pad=0.05)

cbar = plt.colorbar(PC, cax = cax1)
cbar.add_lines(CF)

plt.show()

这篇关于Matplotlib:颜色栏的高度与打印的高度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 08:28