Dman
Dman

Reputation: 145

changing the scale of a matplotlib plot in python

I am trying to make a chart with background as a colormap in python. I am using the following code:

import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt

x = np.arange(1,50,0.01)
y = 1000*np.sin(x)
yarr = np.vstack((x,))
plt.imshow(yarr, extent=(min(x),max(x), min(y),max(y)), cmap=cm.hot)
plt.plot(x, y, color='cornflowerblue',lw=4)

However, this produces an image looking like:

enter image description here

Is there a way to make the graph look 'normal'? I.e. so that I can what is happening (making the axis area more 'square').

Upvotes: 0

Views: 117

Answers (1)

Lescurel
Lescurel

Reputation: 11631

The problem is that plt.imshow() expects "square" pixels, in order to preserve the aspect ratio of the image (see the documentation). Use the option aspect='auto' to get the desired result :

import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt

x = np.arange(1,50,0.01)
y = 1000*np.sin(x)
yarr = np.vstack((x,))
plt.imshow(yarr, extent=(min(x),max(x), min(y),max(y)), cmap=cm.hot, aspect="auto")
plt.plot(x, y, color='cornflowerblue',lw=4)

Matplotlib igure

Upvotes: 1

Related Questions