I have created a sin wave in matplotlib and when I run the code the the window opens and closes immediately without showing anything. What can I do to display the results.
import torch, time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create sine wave data
pi_tensor = torch.linspace(0, 2 * np.pi, 100)
sin_result = torch.sin(pi_tensor)
# Plot the sine wave
plt.plot(pi_tensor.numpy(), sin_result.numpy())
plt.show()
I have created a sin wave in matplotlib and when I run the code the the window opens and closes immediately without showing anything. What can I do to display the results.
import torch, time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create sine wave data
pi_tensor = torch.linspace(0, 2 * np.pi, 100)
sin_result = torch.sin(pi_tensor)
# Plot the sine wave
plt.plot(pi_tensor.numpy(), sin_result.numpy())
plt.show()
By default, if you're running this code in an interactive window, plt.show()
should block until the window is closed. If you're running it in a non-interactive window, it won't block by default, and that is most likely your problem.
Use
plt.show(block=True)
to force it to block.
Ref: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.show.html