This article is a technology blog about Matplotlib Plotting Guide and is written by Prince Grover.

Translation | jann lee Yu Zhipeng | every river


Most people won’t spend a lot of time learning matplotlib and will still be able to implement their drawing needs because there are already solutions to most drawing problems on stackOverflow, Github, and other open source platforms. We usually use Google for our drawing needs. At least I did.

So what’s the point of learning matplotlib? The answer is: it saves searching time. Mastering Matplotlib’s quick list and understanding its basic interface, editing our drawings from a variety of resources to suit our individual needs, will save a lot of time in the long run.

Most of the content is taken from the following 2 links, which I suggest you read as well.

Realpython.com/python-matp…

S3.amazonaws.com/assets.data…

Matplotlib is a Python based 2D drawing library that can draw high graphics across platforms in a variety of hard copy formats and interactive environments.

  • An interesting phenomenon. Why do reference libraries always import matplotlib.pyplot as PLT?

    Because using things like PyLab import * or %pylab is a bad idea, Matplotlib doesn’t recommend it for the following reasons:

From PyLab Import * still exists for historical reasons, but it is strongly recommended not to use it. Doing so can mask Python’s built-in functions and take up namespace space, leading to hard-to-trace bugs. To achieve IPython integration with zero input, the % matplotlib command is recommended. Source: matplotlib.org/users/shell…

Drawing different types of images using Matplotlib is easy, with lots of documentation and tutorials. Most importantly, know the best way to draw. How to use axes, subplots, etc. This article focuses on these issues.

1. Inline plot and % matplotlib

The % matplotlib command enables drawing in the current Notebook. This command provides an optional parameter specifying which matplotlib backend to use. In most cases, an inline background is used in the Notebook, which can embed drawings in the Notebook. Another option is the Qt background, which opens the Matplotlib interactive UI in the side window.

Matlibplot provides a variety of plotting UIs that can be categorized as follows:

  • Popup Windows and interactive interfaces: % matplotlib qt and % matplot tk

  • Non-interactive inline drawing: % matplotlib inline

  • Interactive inline drawing: % matplotlib notebook– > Don’t use this, it makes switching difficult.

2. Understand the structure of matplotlib objects

Pyplot is an object-oriented function interface for Matplotlib.

plt.gca()

It returns the axis associated with the current plot()

If plt.close() is not used, an empty graph is displayed. Because the inline command was used at the beginning.

Axis_id is still the same, but plt.gca() changes when we move to another Notebook block.

Setter and Getter

Getter and Setter methods are used to capture and modify the current or any axies. We may need to modify headings, colors, columns, fonts, etc. There are two ways:

1. Specify axes[I] to capture using fig.. Axes [I] and call axies object using setter’s getter. In the example above, there is only one axes, so we call axes[0].

2. We can call the current axis directly with plt.bla() (where bla can be title(), legend(), xlabel(), etc.). This is an object-oriented function of Matlibplot. This function makes it easy to modify current axes. More commonly used than method 1.

When we use AXES [I], we can call any AXES object in any previous code block, but calling plt.bla() creates a new AXES object in each code block and only calls the current object. Therefore, in the above example, a new PLT object is created only when plt.title() is called.

Important observation: We usually call plt.bla() on the current Axis object, which causes the Axis object in each code block to be newly created. But by calling FIG. Axes [0], we can also process the previous AXES object from any code block.

This is the Stateless (Object Oriented) method and can be customized, which is handy when the image becomes complex.

So, MY suggestion is to use the FIG, ax = plt.subplots(_) first extract axes and figure and assign them to a new variable. These variables can then be used to make changes in the drawing using Getter and Setter methods. Also, this allows us to work on multiple axes instead of just one current axes. Pyplot creates the subplot once, then uses the OO method.

Conclusion: From now on, I use plt.subpots() to complete different drawings. (Please correct me if anyone thinks this is wrong)

3. Matplotlib image analysis

From: matplotlib.org/faq/usage_f…

4. Basic examples of drawing

Basic examples of how to draw, covering all aspects of object-oriented drawing. Please read it carefully.

To summarize the above example:

  • We create a graph with 1 row and 2 columns. That is, 2 axes objects in row 1 and column 2.

  • We customize ax1 and AX2 respectively. As you can see, we can move the y-ticks to the second figure on the right.

5. Drawing of two-dimensional grids

subplot2grid

What needs to be done?

Observe the drawing format below.

The idea is to think of the graph above as a 2×4 grid. Multiple grids are then assigned to a single graph to hold the desired graph.

Key points:

  • We can customize our drawing layout using subplot2Grid.

  • We can create a graph with no axes object using plt.figure() and then manually add axes objects.

  • We can use Fig.supTitle () to set the overall title of the entire figure.

6. Color, color bar, RGB array and color map

We’ve covered ax.plot(), Ax.scatter (), ax.bar() and Ax.hist (). Another more common function is Ax.imshow (), which is used to display color graphs or image/RGB arrays.


7. Line style and line width

Change line width, color, or style.

8. Basic data distribution

Necessary actions in EDA process.

9. Contour plots and color grids for two-dimensional arrays

Thermograms (color grids) and contour maps are useful for visualizing 2D data in many cases.

10. Image adjustment and modification of edge coordinates and scale

Finally adjust the details to make the drawing look better.

11. Scale limitation and automatic adjustment

Things to watch out for:

  • Padding automatically sets the X – or Y-axis grid scale

  • We can use xlim, ylim to set the scale limits for x and y

12. The skill

The axis of the 13.

End of 14.

The blog site: www.kaggle.com/grroverpr/m…

Lei Feng net (public account: Lei Feng net)

Lei Feng net original article, prohibit reprint without authorization. See instructions for details.