用户警告:FigureCanvasAgg 是非交互式的,因此无法显示 plt.show()

问题描述 投票:0回答:1

我正在使用

  • Windows 10
  • PyCharm 2021.3.3 专业版
  • python 3.11.5
  • matplotlib 3.8.1

如何在我的开发环境中永久解决此问题?

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

# Read data from file, skipping the first row (header)
data = np.loadtxt('cm.dat', skiprows=1)

# Initialize reference point
x0, y0, z0 = data[0]

# Compute squared displacement for each time step
SD = [(x - x0)**2 + (y - y0)**2 + (z - z0)**2 for x, y, z in data]

# Compute the cumulative average of SD to get MSD at each time step
MSD = np.cumsum(SD) / np.arange(1, len(SD) + 1)

# Generate time steps
t = np.arange(1, len(SD) + 1)

# Create a log-log plot of MSD versus t
plt.figure(figsize=(8, 6))
plt.loglog(t, MSD, marker='o')

plt.title('Mean Squared Displacement vs Time')
plt.xlabel('Time step')
plt.ylabel('MSD')
plt.grid(True, which="both", ls="--")
plt.show()
C:\Users\pc\AppData\Local\Programs\Python\Python311\python.exe C:/git/RouseModel/tau_plot.py
C:\git\RouseModel\tau_plot.py:29: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown
  plt.show()

Process finished with exit code 0

python matplotlib pycharm
1个回答
1
投票

我也有同样的问题。就我而言,我安装了 PyQt5==5.15.10。之后,我成功运行了我的代码。

pip install PyQt5==5.15.10
pip install PyQt5

© www.soinside.com 2019 - 2024. All rights reserved.