Pycharm使用matplotlib繪圖時報錯
問題描述
TypeError: vars() argument must have __dict__ attribute
源程序:
# -*- encoding: utf-8 -*-
'''
@File : MaLearnTest01_1.py
@Time : 2023/03/03 09:39:05
@Author : seveN1foR
@Version : 1.0
@Contact : sevencdxxiv@qq.com
'''
# here put the import lib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
def draw(FillStyle):
x_coords = np.linspace(-100, 100, 500)
y_coords = np.linspace(-100, 100, 500)
points = []
for y in y_coords:
for x in x_coords:
if ((x * 0.03) ** 2 + (y * 0.03) ** 2 - 1) ** 3 - (x * 0.03) ** 2 * (y * 0.03) ** 3 <= 0: # 引用公式
points.append({"x": x, "y": y})
heart_x = list(map(lambda point: point["x"], points))
heart_y = list(map(lambda point: point["y"], points))
if FillStyle == 1:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5)
else:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5, c=range(len(heart_x)), cmap='autumn')
plt.show()
print(mpl.get_backend())
# 主過程
fStyle = 2
draw(fStyle)
運行結果:
原因分析:
在 PyCharm(至少在科學項目中)使用交互式控制臺運行所有文件,其中使用了后端 module://backend_interagg。 這個后端會導致與您相同的錯誤。所以。 在你的文件頭部添加 mpl.use(‘TkAgg’) ,或者檢查你可以使用哪個后端并在此函數(shù)中傳遞這些名稱。
解決方案:
在你的文件頭部添加 mpl.use(‘TkAgg’) ,或者檢查你可以使用哪個后端并在此函數(shù)中傳遞這些名稱。
但是在某些情況下,TkAgg 可能不可用。 第一次檢查時,您使用的是哪個后端。 為此,運行這個簡單的代碼:
import matplotlib as mpl
print(mpl.get_backend())
但你必須在 PyCharm 之外的默認終端中手動運行。 (例如創(chuàng)建簡單的 test.py 文件,粘貼代碼,然后運行 python test.py)
頭部添加use后的代碼:
# -*- encoding: utf-8 -*-
'''
@File : MaLearnTest01_1.py
@Time : 2023/03/03 09:39:05
@Author : seveN1foR
@Version : 1.0
@Contact : sevencdxxiv@qq.com
'''
# here put the import lib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('TkAgg') # !IMPORTANT 更改在這里?。。。。。。。?!
"""
Why? Because PyCharm (at least in scientific projects) runs all files with an interactive console,
where backend module://backend_interagg is used.
And this backend causes the same error as you have.
So. add mpl.use('TkAgg') in head of your file,
or checkout which backend you can use and past those name in this function.
"""
def draw(FillStyle):
x_coords = np.linspace(-100, 100, 500)
y_coords = np.linspace(-100, 100, 500)
points = []
for y in y_coords:
for x in x_coords:
if ((x * 0.03) ** 2 + (y * 0.03) ** 2 - 1) ** 3 - (x * 0.03) ** 2 * (y * 0.03) ** 3 <= 0: # 引用公式
points.append({"x": x, "y": y})
heart_x = list(map(lambda point: point["x"], points))
heart_y = list(map(lambda point: point["y"], points))
if FillStyle == 1:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5)
else:
plt.scatter(heart_x, heart_y, s=10, alpha=0.5, c=range(len(heart_x)), cmap='autumn')
plt.show()
print(mpl.get_backend())
# 主過程
fStyle = 2
draw(fStyle)
運行結果:
文章來源:http://www.zghlxwxcb.cn/news/detail-515097.html
參考文章源地址:
https://stackoverflow.com/questions/75453995/pandas-plot-vars-argument-must-have-dict-attribute文章來源地址http://www.zghlxwxcb.cn/news/detail-515097.html
到了這里,關于Pycharm使用matplotlib報錯:TypeError: vars() argument must have __dict__ attribute 解決方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!