Matplotlib imshow的选择性模式 [英] Selective patterns with Matplotlib imshow

查看:73
本文介绍了Matplotlib imshow的选择性模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将自定义模式放入 imshow 图形上的选定区域?准确地说,我需要做到这样,除了带有数值数据的彩色方块之外,其他方块中的不同图案也表明了实验的不同失败模式(并且还生成了解释这些含义的密钥)不同的图案).一个有用的模式示例是各种类型的剖面线.我需要能够在不破坏图表上主要颜色数字数据关系的情况下做到这一点.

Is there a way to place custom patterns into selected areas on an imshow graph? To be precise, I need to make it so that, in addition to the numerical-data-carrying colored squares, I also have different patterns in other squares indicate different failure modes for the experiment (and also generate a key explaining the meaning of these different patterns). An example of a pattern that would be useful would be various types of crosshatches. i need to be able to do this without disrupting the main color-numerical data relationship on the graph.

以下是我尝试使用答案中建议的内容的代码.如果我评论错误部分,则无论遮罩没有任何数据,imshow都可以很好地显示带有空白的地方.我什至没有尝试针对不同的故障类型进行不同类型的交叉影线,也没有尝试处理模拟或实验有效但另一个还没有的情况.

Below is code from my attempt to use what was suggested in the answers. If I comment the error section the imshow shows up fine with white space wherever there is no data from the masking. I am not even attempting to do different kinds of crosshatching for different failure types or deal with cases where either the simulation or the experiment worked but the other didn't yet.

我从多处理程序包中收到有关如何腌制"对象的错误消息.由于该程序,这是多处理程序包的一部分.是否有任何方法可以解决此问题或在没有add_patches的情况下执行此操作(以下建议的plot方法不起作用,因为绘图发生在完全不同的坐标系上并绘制了连接线)?

I am receiving error massages from the multiprocessing package about how it 'can't pickle' the object. Due to the program this is a part of it goes through the multiprocessing package. Any way to fix this or do it without add_patches (the plot method suggested below doesn't work, as the plotting happens on a completely different coordinate system and draws connecting lines)?

import numpy as np
import matplotlib.patches as patches
...
grid = np.ma.array(grid, mask=np.isnan(grid))
plot.imshow(grid, interpolation='nearest', aspect='equal', vmax = private.vmax, vmin = private.vmin)
if show_fail and faildat != []:
    faildat = faildat[np.lexsort((faildat[:,yind],faildat[:,xind]))]
    fails = []
    for i in range(len(faildat)):
        fails.append((faildat[i,1],faildat[i,0]))
    for F in fails:
        p = patches.Rectangle(F,1,1,hatch='/',fill=False)
        plot.add_patch(p)
plot.minorticks_off()
plot.set_xticks(range(len(placex)))        
plot.set_yticks(range(len(placey)))
plot.set_xticklabels(placex)        
plot.set_yticklabels(placey, rotation = 0)
plot.colorbar()
plot.show()

推荐答案

执行此操作的方法有多种,更好的方法取决于您是否需要标记较大的区域或散布的单个像素.

There are several ways to do this, which is better will depend on if you need to mark large regions or scattered individual pixels.

如果您需要标记大区域,可以通过在图像上添加矩形来实现:

If you need to mark large regions, you can do this by added rectangles over the image:

import matplotlib as mpl
import matplotlib.pyplot as plt
from numpy.random import rand

ax = plt.gca()
ax.imshow(rand(50,50))
ax.add_patch(mpl.patches.Rectangle((2,2),20,20,hatch='//////////',fill=False,snap=False))
plt.draw()

矩形 (文档)各种哈希选项.这只是在图像顶部添加额外的艺术家,它们不会以任何方式影响数据颜色映射./ 的大量增加了散列标记的密度,这可能是实际看到带有小框的散列所必需的.

Rectangle (doc) a variety of hash options. This is just adding additional artists on top of the image, they will not affect the data-color mapping in any way. The large number of / increases the density of the hash marks, which may be necessary to acctually see the hashing with small boxes.

例如:

from numpy.random import rand
import matplotlib as mpl
import matplotlib.pyplot as plt
     
ax = plt.gca()
ax.imshow(rand(50,50),interpolation='nearest')
for i,j in np.floor(50*rand(10,2)).astype('int'):
    ax.add_patch(mpl.patches.Rectangle((i-.5, j-.5), 1, 1, hatch='///////', fill=False, snap=False))

plt.draw()

如果您只需要在此处标记一些像素,并且可以通过绘图(使用 plot([x],[y],marker ='x')标记大小).

If you just need to mark a few pixels here and you might be able to get away with just plotting (using plot([x],[y],marker='x') and maybe playing with the marker size).

这篇关于Matplotlib imshow的选择性模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆