Pycharm使用matplotlib报错:TypeError: vars() argument must have __dict__ attribute 解决方法

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’) ,或者检查你可以使用哪个后端并在此函数中传递这些名称。


解决方案:

在你的文件头部添加 mpl.use(‘TkAgg’) ,或者检查你可以使用哪个后端并在此函数中传递这些名称。
但是在某些情况下,TkAgg 可能不可用。 第一次检查时,您使用的是哪个后端。 为此,运行这个简单的代码:

import matplotlib as mpl
print(mpl.get_backend())

你必须在 PyCharm 之外的默认终端中手动运行。 (例如创建简单的 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)

运行结果:

在这里插入图片描述

参考文章源地址:

https://stackoverflow.com/questions/75453995/pandas-plot-vars-argument-must-have-dict-attribute

  • 8
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值