Various problems encountered through Pycharm installation package and Matplotlib package installation

1. Install the package through Pycharm

Tip: Installing packages in the system terminal (for example, installing packages through pip in the system terminal) cannot be used directly in Pycharm. Therefore, the following two methods are recommended to install packages, especially method 2. If you don't have Pycharm, download Pycharm , and choose the community version is enough. Pycharm historical version download .

1.1 Method 1

Step 1: Open the pycharm software;
Step 2: Select File → Settings in the menu bar;
Step 3: Select the "Project: XXXXX" column, then select "Project Interpreter", and click the "+" sign on the right;

insert image description here

1.2 Method 2

Take the installation of seaborn as an example, and install it through pip in pycharm, which is more efficient and convenient. Install directly from the system terminal through pip, and pycharm cannot find it, which is troublesome. If pip is not installed, you can install pip through method 1.
insert image description here

1.3 Multiplexing of packages

By default, the packages installed by Pycharm can only be used in the current project, and previously installed packages cannot be used in new projects.
insert image description here
The above two items are unchecked by default. Check the second item to ensure that other projects can reuse the previous package when the first item is checked.

1.4 Common shortcuts

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

2. An error is reported when Pycharm installs the package

Error occurred when installing package 'xxx' pycharm installation library error
My solution is to install pip3 in the terminal, command:

sudo apt install python3-pip

After that, you can use the Pycharm installation package. I have not tried other ways to change the image, such as: successfully resolved the Error occurred when installing package 'Keras' in the setting of pycharm .

3. Matplotlib installation test and problems

Install the Matplotlib package using the method described above, and the test code is as follows:

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 Encountered problems:

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 Solutions

Install python3-tk in the terminal

sudo apt-get install python3-tk

Add a line of code to the py script: matplotlib.use('TkAgg'):

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

Success:
insert image description here
If it doesn't work, you can try to install PyQt5, but I still have problems after installation, and finally solved it by installing python3-tk.

If your problem is not solved, you can further refer to UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." When plotting figure with pyplot on Pycharm Here foreign netizens provide different solutions.

Guess you like

Origin blog.csdn.net/wokaowokaowokao12345/article/details/127106499