Matplotlib imshow() grid uneven when grid size is large

半城伤御伤魂 提交于 2019-12-11 09:45:28

问题


I am trying to use the imshow() function in matplotlib to plot color values in a 177 X 177 grid. When I run the following code on a small (30X30) grid (i.e. I set width = 30), the image that it produces has nice, evenly sized squares. When I run it on a larger grid (150X150), the squares are not evenly proportioned. Some are stretched in one or both dimensions. Here's a simplified version of the code I'm using:

grid = [[]]*width
for i in range(width):
    grid[i] = [[]]*width
    for j in range(width):
        grid[i][j] = (random(), random(), random())

plt.imshow(grid, interpolation="none", aspect=1)
plt.savefig("test.png")

Any idea how to make the grid even? I've tried all of the aspect ratio stuff discussed here, but it didn't seem to solve the problem. I'm using Python 2.7.6 and matplotlib version 1.3.1.

Edit: Here is a picture showing what the cells from the large gird look like if I zoom all the way in. This was obtained using a slightly different coloring function, in which clusters of 9 cells are colored by increasing hue. This more clearly shows the uneven shapes. I can include the code if you want, but I get the same behavior with the (much less complicated) random code.

And here's what it looks (again, zoomed in) like when I use a small grid:


回答1:


Okay, I figured it out. The problem was that I wasn't using a high enough dpi when I saved the image. plt.savefig("test.png", dpi=1000)yields an image in which even a large grid is depicted with evenly-sized squares.

By not increasing the dpi for the larger grid, I was effectively trying to plot more information in the same amount of pixels, so the resolution was decreasing to the point where the cells were becoming miss-shapen.



来源:https://stackoverflow.com/questions/26475642/matplotlib-imshow-grid-uneven-when-grid-size-is-large

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!