0

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').

martineau
124k29 gold badges180 silver badges317 bronze badges
asked Feb 25, 2020 at 15:16

1 Answer 1

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)

Matplotlib igure

answered Feb 25, 2020 at 15:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.