问题
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