rlunde
rlunde

Reputation: 73

Scaling a figure axis in Matplotlib

I'm currently trying to visualize a matrix in Python using Matplotlib, however, the problem is that the number of columns, is much greater than the number of rows, so when i visualize it, the y-axis becomes small, as seen on the following figure. enter image description here

The matrix is a pandas DataFrame with 23 rows and 1880 columns. I've tried to visualize it with imshow() and matshow, but both methods scales the picture awkwardly. I've also attempted tight_layout() but it doesn't really change anything.

Is there anything I can do to scale the plot?

Here is the code i'm using for plotting:

plt.figure()
ax = plt.gca()
plt.imshow(df.drop(["column2"], axis=1).transpose(), cmap='hot', interpolation='nearest')
plt.savefig("Test.pdf")

Upvotes: 0

Views: 198

Answers (2)

Hans
Hans

Reputation: 2615

imshow knows an aspect keyword argument which you can set to auto to do this:

plt.imshow(... , aspect='auto')

Upvotes: 1

Gianluca Micchi
Gianluca Micchi

Reputation: 1653

I found a good aspect ratio using the pcolor function. Your code snippet would become:

plt.pcolor(df.drop(["column2"], axis=1).transpose(), cmap='hot')
plt.savefig("Test.pdf")

Unfortunately, the interpolation keyword is not supported and I don't know if you need it. This is the result I obtain with some random data: visualization with good aspect ratio

Upvotes: 1

Related Questions