python - matplotlib closes window immediately - Stack Overflow

I have created a sin wave in matplotlib and when I run the code the the window opens and closes immedia

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745255740a4618944.html

相关推荐

  • python - matplotlib closes window immediately - Stack Overflow

    I have created a sin wave in matplotlib and when I run the code the the window opens and closes immedia

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信