通过Pycharm安装包以及Matplotlib包安装遇到的各种问题

1. 通过Pycharm安装包

提示: 在系统终端中安装包(例如系统终端中通过pip安装包),在Pycharm中是不能直接使用的。因此,推荐如下两种方法安装包,尤其推荐方法2。如果没有Pycharm,Pycharm下载,选择社区版就够用了。Pycharm历史版本下载

1.1 方法1

第一步:打开pycharm软件;
第二步:选择菜单栏File → Settings;
第三步:选择“Project:XXXXX”这一栏,然后选择“Project Interpreter”,点击右边的“+”号;

在这里插入图片描述

1.2 方法2

以安装seaborn为例,在pycharm中通过pip安装,该方法效率更高更方便。直接从系统终端中通过pip安装,pycharm无法找到,较麻烦。如果没有安装pip,可以通过方法1安装pip。
在这里插入图片描述

1.3 包的复用

默认情况Pycharm安装的包,只能在当前项目中使用,新建的项目无法使用以前安装的包。
在这里插入图片描述
上面两项默认是不勾选的。勾选第二项,就可以确保其他项目勾选第一项的时候能够复用以前的包。

1.4 常用快捷方式

折叠
    Ctrl -: 折叠当前代码
    Ctrl +: 展开当前代码
    Ctrl Shift -: 折叠所有代码
    Ctrl Shift +: 展开所有代码
移动
    Shift+Enter: 在行中间执行时,智能跳到下一行。
    Ctrl+Alt+Enter: 向上插入一行
注释
    Ctrl /: 注释、取消注释行
编辑
    Ctrl + D: 未选中时,复制当前行到下一行,选中时复制粘贴选中部分。
删除
    Ctrl + Y: 删除当前行
查看
    Ctrl + Q: 查看文档
缩进
    Shift + Tab: 反向退格
替换
    Ctrl + r: 替换

2. Pycharm安装包的时候报错

Error occurred when installing package ‘xxx’ pycharm安装库报错
我的解决方法是在终端中安装pip3,命令:

sudo apt install python3-pip

之后通过Pycharm安装包就可以用了。其他改镜像的方式我没有试过,如:成功解决pycharm 的setting中的Error occurred when installing package ‘Keras’

3. Matplotlib安装测试及问题

采用前文描述的方法安装Matplotlib包,测试代码如下:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
             bottom=menMeans, yerr=womenStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.show()

3.1 遇到问题:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.

3.2 解决方法

终端中安装python3-tk

sudo apt-get install python3-tk

在py脚本中增加一行代码:matplotlib.use('TkAgg')

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

成功:
在这里插入图片描述
如果还不行,可以试试安装PyQt5,但是我安装后还是有问题,最终是采用安装python3-tk的方法解决的。

如果没有解决你的问题,可以进一步参考UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm这里国外大神网友提供了不同解决方案。

猜你喜欢

转载自blog.csdn.net/wokaowokaowokao12345/article/details/127106499