当前位置:网站首页>Drawing library Matplotlib installation configuration
Drawing library Matplotlib installation configuration
2022-07-24 21:43:00 【Hua Weiyun】
1. Matplotlib brief introduction
Matplotlib yes Python An important part of the ecosystem , Is a drawing library for visualization , It offers a complete set and matlab Similar orders API And visual interface , It can generate exquisite graphics of publishing quality level ,Matplotlib Make drawing very simple , Excellent balance between ease of use and performance .
2. Matplotlib install
Matplotlib The dependency package and Matplotlib Itself in the standard Python Packages are available in the repository with wheel In the form of documents . therefore , have access to pip The package management tool makes it easy to Matplotlib Installed in the MacOS、Windows、Linux Wait for the system . The installation method is the same as that of many third-party libraries , install Matplotlib Just... On the command line , Execute the following command :
pip install -U matplotlib It should be noted that : To install Matplotlib library , Must have been installed on your computer Python. If you are using Jupyter Notebook, because Jupyter Notebook With many dependent Libraries , Such as Numpy、Pandas、Matplotlib、Scikit Learn etc. , You don't have to install these libraries separately .
After installation, you can use the following command to check , Confirm successful installation :
pip list
3. Matplotlib To configure
After successful installation , That is to say, you can Python Import and use... Like other libraries in Matplotlib, Configuration without more files , Usually we import it and use an alias mpl:
import matplotlib as mplMatplotlib The configuration of is more used to modify the default style of drawing graphics ,Matplotlib The configuration file contains various default graphic configuration information , We can customize the drawing style by modifying these configuration information and global parameters , These parameters can change the size of the drawing 、 The color scheme 、 Font and a series of information .
It can be done in many ways Matplotlib Drawing configuration , This paper mainly introduces the following three configuration methods The configuration file To configure 、 adopt rcParams['param_name'] Dynamic configuration and passing matplotlib.rc() Function configuration .
3.1 Configure through the configuration file
Configuration files can also be divided into several different levels , If we want to change the default configuration used by all graphics , You need to modify the global default configuration ; And if we need to use different configurations according to different tasks , Or different users use different configurations , You need to modify the local configuration file , To be able to use different graphic configurations in different users and tasks . According to the scope of the configuration file , It can be divided into three levels : Global profile 、 User level profile and current task profile .
Files at three levels of different systems are located in different directories , You can use the following code , View the path to the profile :
import matplotlib as mplimport os# Global configuration directory print(mpl.__path__)# Current user configuration directory print(mpl.get_configdir())# Current task configuration directory , That is, the current code running Directory print(os.getcwd())- Global profile
mpl-data\matplotlibrc, be locatedMatplotlibInstallation directory of , For example, inWindowInstall it underD:\Program Files\Python39\lib\site-packages\matplotlibUnder the table of contents , The full file name of the global configuration file isD:\Program Files\Python39\lib\site-packages\matplotlib\mpl-data\matplotlibrc, By default , Drawings are drawn using this profile . - User level profiles
.matplotlib\matplotlibrc, Located under the user directory , for example , The user directory isC:\Users\Brainiac\, Then the corresponding configuration file isC:\Users\Brainiac\.matplotlib\matplotlibc; If this file does not exist , It can also be created and modified according to the global configuration file and user requirements . - Current task profile
matplotlibrc, That is, it is located under the code running Directory , It can be used to customize the code of the current taskMatplotlibTo configure , By default, this file does not exist , That is, the global or current user profile is used by default , We can create this file as needed , And configure as needed .
After introducing the location of the configuration file , By looking at the global configuration file , Observe the relevant graphic attributes that can be configured in the configuration file , The following is an example of a global configuration file :

NOTE: As you can see, it is not recommended to modify the global configuration file directly , You can copy this file to the user and profile directory or the current task profile directory , And make changes as needed . The format of configuration file is generally Property name : Property value , The line width is configured as follows 1.5:
lines.linewidth: 1.53.2 adopt rcParams[‘param_name’] To configure
And if we just want to simply modify the custom configuration in the current file , You can use the rcParams['param_name'] Faster modifications . By using the following code , You can view the properties that can be customized :
import matplotlib as mpl# You can use the following three ways print(mpl.rc_params())print(mpl.rcParamsDefault)print(mpl.rcParams) The output result is similar to that in the configuration file , The format is also Property name : Property value :
...font.size: 10.0font.stretch: normalfont.style: normalfont.variant: normalfont.weight: normal... Use rcParams['param_name'] The way to modify the configuration is as follows , among param_name Represents the property name :
import matplotlib as mpl# Change the line width to 2mpl.rcParams['lines.linewidth'] = 2# Change the line color to red mpl.rcParams['lines.color'] = 'r'in application , The two most commonly used configurations include the display of Chinese and Chinese minus signs , If not configured , Chinese and Chinese minus signs are not supported by default :

Configure in the following way :
import matplotlib as mpl# According to Chinese mpl.rcParams['font.sans-serif'] = ['SimHei']# Show minus sign mpl.rcParams['axes.unicode_minus']=FalseAfter configuration, the graphics can display Chinese and Chinese symbols normally :

3.3 adopt matplotlib.rc() Function configuration
We can also use matplotlib.rc() Function to configure , How to use it is as follows :
import matplotlib as mpl# Modify lineweight mpl.rc('lines', linewidth=2, color='g') among rc The first argument to the function is group Represents the group to which the attribute belongs , Scope used to qualify attributes , For example, in the above example, the line width linewidth Belong to line lines Used to limit the effect only in the line bar , And for the line width of the coordinate axis linewidth It doesn't work , If you want to change the line width of the drawing including the coordinate axis linewidth You need to use :
import matplotlib as mpl# Change the lineweight of the entire drawing mpl.rc('axes', linewidth=2)边栏推荐
- P2404 splitting of natural numbers
- Node installation using NVM succeeded, but NPM installation failed (error while downloading, TLS handshake timeout)
- How to drain the applet correctly? Three positions of whole network drainage!
- What problems should be paid attention to when using a database without public ip: port?
- Information system project manager must recite the core examination site (47) project subcontract
- Huawei cloud data governance production line dataarts, let "data 'wisdom' speak"
- 731. My schedule II (segment tree or scoring array)
- Dtable launched in the public beta, which is not only a table, but also a business application builder
- Codeforces Round #808 (Div. 2)(A~D)
- Atcoder beginer contest 260 a~f problem solution
猜你喜欢

运动控制如何位置同步输出

Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry

Nested printing in CAD web pages

Codeforces Round #809 (Div. 2)(A~D2)

Together again Net program hangs dead, a real case analysis of using WinDbg

Classical review: understanding the "knowledge consistency" of neural networks (ICLR 2020)

Node installation using NVM succeeded, but NPM installation failed (error while downloading, TLS handshake timeout)

Shell introduction and variable definition

Information System Project Manager - Chapter 10 project communication management and project stakeholder management

Using skills and design scheme of redis cache (classic collection version)
随机推荐
Mathpix formula extractor
ECCV 2022 open source | target segmentation for 10000 frames of video
How much does it cost to build your own personal server
Big country "grain" policy | wheat expert Liu Luxiang: China's rations are absolutely safe, and the key to increasing grain potential lies in science and technology
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]
Uniqueness and ordering in set
Shell introduction and variable definition
How do test / development programmers survive the midlife crisis? You can see it at a glance
Together again Net program hangs dead, a real case analysis of using WinDbg
Information System Project Manager - Chapter 10 project communication management and project stakeholder management
[image processing] pyefd.elliptic_ fourier_ How descriptors are used
Discussion on solving the application ecological problems of domestic systems based on small programs
HSPF (hydraulic simulation program FORTRAN) model
中信证券股票开户怎么样安全吗
Want to open an account and fry American crude oil, but always worry about insecurity?
P2404 splitting of natural numbers
Dtable launched in the public beta, which is not only a table, but also a business application builder
731. My schedule II (segment tree or scoring array)
Rce (no echo)
Alibaba cloud and parallel cloud launched the cloud XR platform to support the rapid landing of immersive experience applications