leoric666 发表于 2020-10-21 12:42:36

求大神帮忙解惑matplotlib的错误

import matplotlib.pyplot as plt

x_values = list(range(1, 1001))
y_values =

plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolor='none', s=40)

# Set chart title, and label axes.
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)

# Set size of tick labels.
plt.tick_params(axis='both', which='major', labelsize=14)

# Set the range for each axis.
plt.axis()

plt.show()



UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()

疾风怪盗 发表于 2020-10-21 13:04:26

运行,没有报你说的错,就一个提醒,好像是颜色问题

如果你说的那个错误
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend
解决:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

百度搜一下,第一个文章就是https://blog.csdn.net/qq_41073715/article/details/105033751

leoric666 发表于 2020-10-21 14:13:16

本帖最后由 leoric666 于 2020-10-21 14:17 编辑

疾风怪盗 发表于 2020-10-21 13:04
运行,没有报你说的错,就一个提醒,好像是颜色问题

如果你说的那个错误


C:\Python32\venv\Scripts\python.exe C:/Python32/python_code/code/chapter_15/scatter_squares.py
Traceback (most recent call last):
File "C:/Python32/python_code/code/chapter_15/scatter_squares.py", line 3, in <module>
    import matplotlib.pyplot as plt
File "C:\Python32\venv\lib\site-packages\matplotlib\pyplot.py", line 2336, in <module>
    switch_backend(rcParams["backend"])
File "C:\Python32\venv\lib\site-packages\matplotlib\pyplot.py", line 276, in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
File "C:\Python32\venv\lib\site-packages\matplotlib\pyplot.py", line 277, in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
File "C:\Users\lizhou li\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name, package, level)
File "C:\Python32\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1, in <module>
    from . import _backend_tk
File "C:\Python32\venv\lib\site-packages\matplotlib\backends\_backend_tk.py", line 21, in <module>
    from . import _tkagg
ImportError: cannot import name '_tkagg' from 'matplotlib.backends' (C:\Python32\venv\lib\site-packages\matplotlib\backends\__init__.py)

Process finished with exit code 1

这是我加了您给的提示后出现的错误,我之前也试过一直不行,我也不知道为什么,之前还能出图像然后就不行了

疾风怪盗 发表于 2020-10-21 14:17:54

leoric666 发表于 2020-10-21 14:13
C:\Python32\venv\Scripts\python.exe C:/Python32/python_code/code/chapter_15/scatter_squares.py
Tr ...

用你的代码,没加这句话matplotlib.use('TkAgg'),我这边也照样出图了
你要么卸载了matplotlib,再安装一次试试看

leoric666 发表于 2020-10-21 14:19:46

疾风怪盗 发表于 2020-10-21 14:17
用你的代码,没加这句话matplotlib.use('TkAgg'),我这边也照样出图了
你要么卸载了matplotlib,再安装 ...

我试过在python终端上删除matplotlib但是好像对pycharm没影响,请问您知道怎么在pycharm删除这个matplotlib吗

疾风怪盗 发表于 2020-10-21 14:26:06

leoric666 发表于 2020-10-21 14:19
我试过在python终端上删除matplotlib但是好像对pycharm没影响,请问您知道怎么在pycharm删除这个matplotl ...

怎么可能没影响,是不是装了多个python?pycharm只是个工具,运行的还是python,如果你真的卸载了matplotlib,那运行时就显示没有这个模块了

leoric666 发表于 2020-10-21 14:37:55

疾风怪盗 发表于 2020-10-21 14:26
怎么可能没影响,是不是装了多个python?pycharm只是个工具,运行的还是python,如果你真的卸载了matplotli ...

好的哈 我再试试,谢谢你了哈
页: [1]
查看完整版本: 求大神帮忙解惑matplotlib的错误