简体   繁体   中英

Matplotlib | Python | Plotting figure A and then plotting another figure B on top of figure A

The fragment of code below plots a figure based on two arrays of floats

plt.scatter(t, h)
plt.xlabel("time")
plt.ylabel("height")
plt.show()

Then, after defining a function y(t), I need to add the following on top of the last plot:

plt.plot(t, y(t), 'r')
plt.show()

However, the code above generates two separate plots. I've noticed that if I comment out the first plt.show() , I'll get the second figure I am looking for. But is there a way to show them both?

I was expecting one plot and then another plot on top of the second one; however the second plot is shown as a new one

plt.show() draws your figure on screen.

So, you need to remove the first plt.show() from your code.

If you remove the first plt.show() you should see the joint plot. The first plt.scatter just produces points on the joints of the line.

import numpy as np
import matplotlib.pyplot as plt
t = np.random.random(10)
h = np.random.random(10)
plt.scatter(t, h)
plt.plot(t, h, 'r')
plt.show()

The scatter plot is just the blue dots

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM