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

Time:03-28

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

CodePudding user response:

plt.show() draws your figure on screen.

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

CodePudding user response:

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

  • Related