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

这篇具有很好参考价值的文章主要介绍了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使用matplotlib报错:TypeError: vars() argument must have __dict__ attribute 解决方法


原因分析:

在 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)

运行结果:

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

参考文章源地址:

https://stackoverflow.com/questions/75453995/pandas-plot-vars-argument-must-have-dict-attribute文章来源地址https://www.toymoban.com/news/detail-515097.html

到了这里,关于Pycharm使用matplotlib报错:TypeError: vars() argument must have __dict__ attribute 解决方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【已解决】Flask项目报错TypeError: tuple indices must be integers or slices, not str

    本解决方案适用情境 :在 本地可以正常运行 的flask项目, 放到云服务器报错 TypeError: tuple indices must be integers or slices, not str,即代码本身无误的前提,可能因为环境差异导致的问题。 报错代码 TypeError: tuple indices must be integers or slices, not str 这个错误的意思是元组索引必须是整

    2024年02月17日
    浏览(31)
  • webdriver报错:TypeError: __init__() got an unexpected keyword argument ‘executable_path‘已解决

    webdriver报错:TypeError: init () got an unexpected keyword argument \\\'executable_path’已解决 错误段代码如下: 先说一下我下载的chromedriver版本是122.0.6261.94,chrome的版本是122.0.6261.112 selenium或selenium下的webdriver的库版本的原因,与chromedriver的版本不匹配。 终端执行pip show selenium查看版本。我报

    2024年04月25日
    浏览(32)
  • Pytorch报错TypeError : __init__() takes 1 positional argument but 2 were given 原因及解决方法

    问题 : Pytorch报错TypeError : __init__() takes 1 positional argument but 2 were given 解决方法 : 在网上搜了下,都是说自己的模型定义错误,我看了下,发现也没有错误,就很懵! 然后看看之前的代码发现我没有实例化!!! 贴代码 : 这个模块没有毛病。 可总是报错: __init__() takes

    2024年02月13日
    浏览(35)
  • rabbitmq集群搭建报错:[error] Cookie file /var/lib/rabbitmq/.erlang.cookie must be accessible by owner only

    在创建rabbitmq集群时,需要将当前节点的.erlang.cookie文件数据修改为第一个节点的.erlang.cookie文件内容,这里为了防止手动vim修改导致数据末尾的自动换行符的引入,我使用了文件的直接替换,随后在重启当前的mq节点服务时,报错如下: 结果就是rabbitmq启动失败 随后,执行

    2024年02月15日
    浏览(31)
  • vue/cli@4执行npm run build报错:Syntax Error: Thread Loader (Worker 2) The “from“ argument must be of

    目录 1、问题 2、原因 3、解决方案 Syntax Error: Thread Loader (Work 2) The \\\"from\\\" argument must be of type string. Received undefined 语法错误:线程加载器(工作2) “from”参数的类型必须为字符串。接收未定义 错误提示代码: vue-cli 使用 wokrer-loader 加载 web woker 时,使用 npm run build 有很大机

    2024年02月09日
    浏览(40)
  • 已解决(selenium操作火狐浏览器报错)TypeError: __init__() got an unexpected keyword argument ‘firefox_options‘

    已解决(selenium操作火狐浏览器报错)TypeError: init () got an unexpected keyword argument ‘firefox_options‘ 粉丝群里面的一个小伙伴想用selenium操作火狐浏览器,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇

    2024年02月09日
    浏览(37)
  • 【python】pycharm 2023.02导入matplotlib报错

    换新电脑、重装系统后,新装的pycharm 2023.02版本导入 matplotlib 会报错:找不到指定的模块 需要重新安装 Microsoft Visual C++ 2015 Redistributable Update 3或更高版本 下载链接:Download Microsoft Visual C++ 2015 Redistributable Update 3 from Official Microsoft Download Center

    2024年02月13日
    浏览(22)
  • 【学习】python之使用pandas提示TypeError: NDFrame.to_excel() got an unexpected keyword argument ‘encoding‘

    桨桨,终于有东西可以来记录解决的问题点啦~ 背景是在使用pandas一直无法转换成excel,排查了很久,终于在做了一个细微的调整实现成功了。 pandas 是基于NumPy 的一种工具。我的理解:这个包可以实现读取excel,写入excel的功能,分别是readexcel,toexcel。 运行报错提示 Traceback (m

    2024年04月08日
    浏览(78)
  • PyCharm使用matplotlib:报MatplotlibDeprecationWarning.问题已解决

    这个错误全部显示为:MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later. 报错截图如下所示: 代码示例: import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 10, 0.1) y = np.sin(x) plt.plot

    2024年02月06日
    浏览(23)
  • 修复 Python 错误TypeError: Missing 1 Required Positional Argument

    类是面向对象编程语言的基本特征之一。 每个对象都属于 Python 中的某个类。 我们可以创建我们的类作为蓝图来创建相同类型的对象。 我们使用 class 在 Python 中定义一个类。 Python 中一个非常重要的特性是在定义类时使用 self 属性。 self 属性表示对象的数据并将参数绑

    2024年02月10日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包