繁体   English   中英

使用matplotlib在图像文件上绘制半透明等高线图

[英]Plot semi transparent contour plot over image file using matplotlib

我想在matplotlib / pyplot中的图像文件上绘制透明等高线图。

这是我到目前为止所得到的......

我有一个test.png像素的方形图像文件test.png看起来像这样:

在此输入图像描述

我想使用matplotlib和pyplot在该图像上绘制轮廓图(使图像文件在'下方'并且覆盖轮廓图的半透明版本)。 作为奖励,图像将自动缩放以适合当前的绘图边界。 我的示例绘图脚本如下:

from matplotlib import pyplot
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
from pylab import *
import numpy as np
import random

# ----------------------------- #

dx, dy = 500.0, 500.0
y, x = np.mgrid[slice(-2500.0, 2500.0 + dy, dy),slice(-2500.0, 2500.0 + dx, dx)]

z = []
for i in x:
    z.append([])
    for j in y:
        z[-1].append(random.uniform(80.0,100.0))

# ----------------------------- #

plot_aspect = 1.2
plot_height = 10.0
plot_width = int(plot_height*plot_aspect)

# ----------------------------- #

pyplot.figure(figsize=(plot_width, plot_height), dpi=100)
pyplot.subplots_adjust(left=0.10, right=1.00, top=0.90, bottom=0.06, hspace=0.30)
subplot1 = pyplot.subplot(111)

# ----------------------------- #

cbar_max = 100.0
cbar_min = 80.0
cbar_step = 1.0
cbar_num_colors = 200
cbar_num_format = "%d"

# ----------

levels = MaxNLocator(nbins=cbar_num_colors).tick_values(cbar_min, cbar_max)
cmap = pyplot.get_cmap('jet')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap)
cbar = pyplot.colorbar(pp, orientation='vertical', ticks=np.arange(cbar_min, cbar_max+cbar_step, cbar_step), format=cbar_num_format)
cbar.ax.set_ylabel('Color Scale [unit]', fontsize = 16, weight="bold")

# ----------

CS = pyplot.contour(x,y,z, alpha=0.5)

# ----------

majorLocator1   = MultipleLocator(500)
majorFormatter1 = FormatStrFormatter('%d')
minorLocator1   = MultipleLocator(250)

subplot1.xaxis.set_major_locator(majorLocator1)
subplot1.xaxis.set_major_formatter(majorFormatter1)
subplot1.xaxis.set_minor_locator(minorLocator1)

pyplot.xticks(fontsize = 16)
pyplot.xlim(-2500.0,2500.0)

# ----------

majorLocator2   = MultipleLocator(500)
majorFormatter2 = FormatStrFormatter('%d')
minorLocator2   = MultipleLocator(250)

subplot1.yaxis.set_major_locator(majorLocator2)
subplot1.yaxis.set_major_formatter(majorFormatter2)
subplot1.yaxis.set_minor_locator(minorLocator2)

pyplot.yticks(fontsize = 16)
pyplot.ylim(-2500.0,2500.0)

# ----------

subplot1.xaxis.grid()
subplot1.yaxis.grid()

# ----------

subplot1.axes.set_aspect('equal')

# ----------

pyplot.suptitle('Main Title', fontsize = 24, weight="bold")

# ----------

pyplot.xlabel('X [m]', fontsize=16, weight="bold")
pyplot.ylabel('Y [m]', fontsize=16, weight="bold")

# ----------

implot = subplot1.imshow( pyplot.imread('test.png') , interpolation='nearest', alpha=0.5)

# ----------
pyplot.show()
#pyplot.savefig("tmp.png", dpi=100)
pyplot.close()

......但我没有得到我想要的结果......相反,我只看到了轮廓图部分。 就像是:

在此输入图像描述

我应该怎么做我的代码才能得到我想要的东西?

你基本上需要做两件事,在背景中设置你想要的图像范围。 如果你不这样做,坐标被认为是像素坐标,在这种情况下,x和y都是0到600。 所以调整你的imshow命令:

implot = subplot1.imshow(pyplot.imread(r'test.png'), interpolation='nearest', 
                         alpha=0.5, extent=[-2500.0,2500.0,-2500.0,2500.0])

如果要将图像自动拉伸到绘图的极限,可以使用以下方式获取范围:

extent = subplot1.get_xlim()+ subplot1.get_ylim()

并将其传递给imshow作为extent=extent

由于它的背景图像,将alpha设置为0.5会使它非常微弱,我会将其设置为1.0。

其次,您设置轮廓线的alpha,但您也可能(或特别)想要设置filled contours的alpha。 当您使用填充轮廓的alpha时,启用抗锯齿可以减少伪影。 所以将contourf命令更改为:

pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap, alpha=.5, antialiased=True)

由于您已经自己创建了子图对象,我建议也使用它来进行绘图而不是在当前活动轴上运行的pyplot界面。

所以:

subplot1.contourf()
etc

代替:

pyplot.contourf()

通过上面提到的两个更改,我的结果如下:

在此输入图像描述

暂无
暂无

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

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