当前位置:网站首页>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)边栏推荐
- Alibaba cloud and parallel cloud launched the cloud XR platform to support the rapid landing of immersive experience applications
- Dtable launched in the public beta, which is not only a table, but also a business application builder
- 90% of people don't know the most underestimated function of postman!
- Documentary of the second senior brother
- Scientific computing toolkit SciPy image processing
- Lenovo Filez helps Zhongshui North achieve safe and efficient file management
- Strong reference, weak reference, soft reference, virtual reference
- Little Red Book Keyword Search commodity list API interface (commodity detail page API interface)
- 731. My schedule II (segment tree or scoring array)
- [blind box app mall system] function introduction after online unpacking
猜你喜欢

How does redis realize inventory deduction and prevent oversold? (glory Collection Edition)
![[image processing] pyefd.elliptic_ fourier_ How descriptors are used](/img/72/d2c825ddd95f541b37b98b2d7f6539.png)
[image processing] pyefd.elliptic_ fourier_ How descriptors are used

Lenovo Filez helps Zhongshui North achieve safe and efficient file management

小程序地理位置接口申请

C synchronous asynchronous callback state machine async await demo

Node installation using NVM succeeded, but NPM installation failed (error while downloading, TLS handshake timeout)
![[jzof] 06 print linked list from end to end](/img/c7/c2ac4823b5697279b81bec8f974ea9.png)
[jzof] 06 print linked list from end to end

How to prevent weight under Gao Bingfa?

Gather relevant knowledge points and expand supplements

Understand MySQL index and b+tree in an easy to understand way (supreme Collection Edition)
随机推荐
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
Mysql database query is so slow. Besides index, what else can it do?
From front-line development to technical director, you are almost on the shelf
Uniqueness and ordering in set
Nested printing in CAD web pages
巧妙使用sort(List<T>,Comparator<? super T>)比较器
Mathpix formula extractor
[image processing] pyefd.elliptic_ fourier_ How descriptors are used
Go language pack management
What is a database password?
01_ UE4 advanced_ PBR material
IO flow overview
陈春花与莫言,都有苦难言
Bring new people's experience
Leetcode skimming -- bit by bit record 018
[install PG]
Metauniverse: technological evolution, industrial ecology and big country game
Is it safe to open an account on Alipay
What should I pay attention to when selecting DTS database type?
String matching (Huawei)