Home >Backend Development >Python Tutorial >Python visual learning: Detailed introduction to Matplotlib configuration

Python visual learning: Detailed introduction to Matplotlib configuration

高洛峰
高洛峰Original
2017-03-26 16:29:522444browse

Matplotlib is an excellent visualization library. It provides a rich interface, making Python visualization very easy to use. This series is my study notes for learning python visualization. It is mainly used to monitor my learning progress. I also hope to communicate with related bloggers.
Since I am a working data analyst rather than a developer, learning Python visualization is mainly to solve the problems of automatic data visualization encountered in my daily work. Therefore, the learning plan is to start from the basics (experts should not criticize), and the book used is "Python Visual Programming Practice". The entire study plan is to first go through the basics according to this book, and then check the manual or search for specialized solutions based on the problems encountered during actual use.

As the first chapter of this series, I focus on recording the configuration methods and contents of matplotlib.

1. Configuration of Matplolib default parameters

  • Many parameters of Matplotlib are configured through .rc files, and have been configured for most attributes The default value is configured. You can make necessary adjustments to various default attributes according to your needs.

  • During the execution of the code, you can change the running parameters in two ways.

    • Use a dictionary (rcParams) to configure parameters: call rcParams (a dictionary) to adjust the corresponding parameters in the dictionary during the running of the code.

    • Call matplotlib.rc() Function: Modify configuration items by passing in attribute tuples to matplotlib.rc().

    • If you need to configure dynamically modified configuration parameters, you can call matplotlib.rcdefaults() to reset the configuration to the standard configuration.
      The following two pieces of code achieve the same effect, but the methods are different:

      # 采用的matplotlib.rcParams的例子:import matplotlib as mpl
      mpl.rcParams['lines.width']=2 #将线宽设置为2mpl.rcParams['lines.color']='r' #将线的颜色设置为红色 red#使用matplotlib.rc() 的例子mpl.rc('lines',linewidth=2,color='r')

2. Configure matplotlib parameters for the project

Use The way to configure parameters is to allow you to use different parameter configurations in different projects and distribute configuration templates among different colleagues and projects.
matplotlib is configured through the matplotrc file. This file can have three levels according to their application scope:

  1. Current working directory: the directory where the code is run, which can be the directory Contains current project code that customizes matplotlib configuration items. The name of the configuration file is: matplotlibrc

  2. User-level configuration file: .matplotlib/matplotlibrc file. Usually in the user's $HOME (that is, the Documents and Setting\ directory in the Windows system). You can call the matplotlib.get_configdir() command to obtain the current user's configuration file directory.

  3. Install the level configuration file: usually in the site-packags directory of python. However, this file will be overwritten every time matplotlib is reinstalled, so if you want to maintain a persistent and effective configuration, it is best to configure it in a user-level configuration file.


    The configuration file includes the following options (Object):

  • axes: Set the axis boundaries and Color, coordinate scale value size and grid display;

  • backend: Set the target output TkAgg and GTKAgg.

  • figure: Control dpi, border color, graphic size and subplot settings.

  • font: Font set (font-family), font size and style settings.

  • grid: Set the color and line style of the grid.

  • legend: Set the display mode of the legend and its text.

  • line: Set lines (color, line style, line width, etc.) and markers.

  • patch: is a graphic object that fills 2D space, such as polygons and circles. Control line width, color, anti-aliasing settings, and more.

  • savefig: Saved graphics can be set individually, for example, the color background of the rendered output image is white.

  • text: Set the font color, text parsing, etc.

  • verbose: Set the information output of matplotlib during execution, such as silent, helpful, debug, etc.

In fact, these objects are separate objects in matplotlib and have separate API. In the process of drawing with Matplotlib, various configured objects are actually piled together.

The above is the detailed content of Python visual learning: Detailed introduction to Matplotlib configuration. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn