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:
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').
martineau
124k29 gold badges180 silver badges317 bronze badges
1 Answer 1
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)
answered Feb 25, 2020 at 15:32
lang-py