admin管理员组

文章数量:1416651

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()     
Share Improve this question asked Feb 2 at 21:29 Sharks CharlatansSharks Charlatans 211 silver badge2 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

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./stable/api/_as_gen/matplotlib.pyplot.show.html

本文标签: pythonmatplotlib closes window immediatelyStack Overflow