【问题标题】:Python vertical lines in countour plot等高线图中的Python垂直线
【发布时间】:2017-04-10 20:36:33
【问题描述】:

在二维绘图中,我通常使用

l, = pylab.plot([10, 10], [-1000,1000], color="g", lw=0.5) 在 x 位置 10 处绘制垂直线,垂直轴范围从 -1000 到 1000。

我想在等高线图中做同样的事情 如何在等高线图中绘制垂直线?

特别是我正在使用下面的代码

# Blue-White-Red colorbar for plots with negative and positive values.
cdict = {'red':   ((0.0, 0.0, 0.0),
                   (0.5, 1.0, 1.0),
                   (1.0, 1.0, 1.0)),
         'green': ((0.0, 0.0, 0.0),
                   (0.5, 1.0, 1.0),
                   (1.0, 0.0, 0.0)),
         'blue':  ((0.0, 1.0, 1.0),
                   (0.5, 1.0, 1.0),
                   (1.0, 0.0, 0.0))}
my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', cdict, 1024)

fig = pylab.figure(figsize=(3.46,2.14), frameon=False)
pylab.axes([0.17, 0.20, 0.50, 0.75])
p1 = pylab.imshow(
    dens.transpose()*10000.0,
    cmap=my_cmap, aspect='auto',
    interpolation='bicubic',
    vmin=-2.0, vmax=2.0,
    extent=(times[0],times[1],y[0]/1000.0,y[-1]/1000.0))
pylab.xlabel(r"$t\,[{\rm ps}]$")
pylab.ylabel(r"$y\,[\mu{\rm m}]$")
pylab.xlim([tmin,tmax])
#pylab.ylim(eRange)
ax2 = pylab.gcf().add_axes([0.7, 0.2, 0.1, 0.75])
pylab.colorbar(mappable=p1,cax=ax2)
#pylab.axvline(x=5.0, color='k', linestyle='--')
ax2.xaxis.set_ticks([])
ax2.yaxis.tick_right()
pylab.figtext(0.75, 0.10, r"$\times 10^{-4}$")

contour plot example

如何在 t = 5 处绘制一条垂直线?

上面的注释行不起作用。

【问题讨论】:

  • “不起作用”到底是什么意思?你有错误吗?

标签: python matplotlib lines contour


【解决方案1】:

以下使用imshowcontour 的代码运行良好。 它使用精确的在两点之间绘制一条线。

import matplotlib.pyplot as plt
import numpy as np

z = np.random.rand(21,21)

fig, (ax, ax2)= plt.subplots(ncols=2, figsize=(7,3))
ax.imshow(z, extent=[0,20,0,20], cmap="viridis")
ax.plot([5,5],[0,20], lw=10, c="r")

ax2.contour(z)
ax2.plot([5,5],[0,20], lw=10, c="r")

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-09-25
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多