[英]Aligning two combined plots - Matplotlib
I'm currently working in a plot in which I show to datas combined. 我目前在一个情节中工作,在该情节中我将显示合并数据。 I plot them with the following code: 我用以下代码绘制它们:
plt.figure()
# Data 1
data = plt.cm.binary(data1)
data[..., 3] = 1.0 * (data1 > 0.0)
fig = plt.imshow(data, interpolation='nearest', cmap='binary', vmin=0, vmax=1, extent=(-4, 4, -4, 4))
# Plotting just the nonzero values of data2
x = numpy.linspace(-4, 4, 11)
y = numpy.linspace(-4, 4, 11)
data2_x = numpy.nonzero(data2)[0]
data2_y = numpy.nonzero(data2)[1]
pts = plt.scatter(x[data2_x], y[data2_y], marker='s', c=data2[data2_x, data2_y])
And this gives me this plot: 这给了我这个情节:
As can be seen in the image, my background and foreground squares are not aligned. 从图像中可以看出,我的背景和前景方块未对齐。
Both of then have the same dimension (20 x 20). 两者的尺寸相同(20 x 20)。 I would like to have a way, if its possible, to align center with center, or corner with corner, but to have some kind of alignment. 我希望有一种方法,如果可能的话,将中心与中心对齐,或将角与角对齐,但要进行某种对齐。
In some grid cells it seems that I have right bottom corner alignment, in others left bottom corner alignment and in others no alignment at all, with degrades the visualization. 在某些网格单元中,我似乎具有右下角对齐,在其他网格中左下角对齐,而在其他根本没有对齐,这会降低可视化效果。
Any help would be appreciated. 任何帮助,将不胜感激。
Thank you. 谢谢。
As tcaswell
says, your problem may be easiest to solve by defining the extent
keyword for imshow
. 作为tcaswell
说,你的问题可能是最容易通过定义来解决extent
关键字imshow
。
If you give the extent keyword
, the outermost pixel edges will be at the extents. 如果您提供extent keyword
,则最外面的像素边缘将位于该范围内。 For example: 例如:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.random.random((8, 10)), extent=(2, 6, -1, 1), interpolation='nearest', aspect='auto')
Now it is easy to calculate the center of each pixel. 现在很容易计算每个像素的中心。 In X direction: 在X方向:
Similarly, the Y centers are at -.875 + n * 0.25. 同样,Y中心为-.875 + n * 0.25。
So, by tuning the extent
you can get your pixel centers wherever you want them. 因此,通过调整extent
您可以将像素中心放置在任何需要的位置。
An example with 20x20 data: 一个具有20x20数据的示例:
import matplotlib.pyplot as plt
import numpy
# create the data to be shown with "scatter"
yvec, xvec = np.meshgrid(np.linspace(-4.75, 4.75, 20), np.linspace(-4.75, 4.75, 20))
sc_data = random.random((20,20))
# create the data to be shown with "imshow" (20 pixels)
im_data = random.random((20,20))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(im_data, extent=[-5,5,-5,5], interpolation='nearest', cmap=plt.cm.gray)
ax.scatter(xvec, yvec, 100*sc_data)
Notice that here the inter-pixel distance is the same for both scatter
(if you have a look at xvec
, all pixels are 0.5 units apart) and imshow
(as the image is stretched from -5 to +5 and has 20 pixels, the pixels are .5 units apart). 请注意,此处的scatter
(如果您查看xvec
,所有像素都相隔0.5个单位)和imshow
(图像从-5拉伸到+5且有20个像素,像素相隔.5个单位)。
here is a code where there is no alignment problem. 这是没有对齐问题的代码。
import matplotlib.pyplot as plt
import numpy
data1 = numpy.random.rand(10, 10)
data2 = numpy.random.rand(10, 10)
data2[data2 < 0.4] = 0.0
plt.figure()
# Plotting data1
fig = plt.imshow(data1, interpolation='nearest', cmap='binary', vmin=0.0, vmax=1.0)
# Plotting data2
data2_x = numpy.nonzero(data2)[0]
data2_y = numpy.nonzero(data2)[1]
pts = plt.scatter(data2_x, data2_y, marker='s', c=data2[data2_x, data2_y])
plt.show()
which gives a perfectly aligned combined plots: 给出了完美对齐的组合图:
Thus the use of additional options in your code might be the reason of the non-alignment of the combined plots. 因此,在代码中使用其他选项可能是组合图未对齐的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.