SoFunction
Updated on 2024-11-07

python plotting subgraph techniques, and axis modification

preamble

I stumbled upon the fact that there are two ways to plot subgraphs in python (matplotlib), one is, the other is, this blog talks about the difference between these two methods, their usage, and some of the commonly used functions.

The role is to define a large drawing, you can set the size of the drawing, resolution, etc., for example

fig = (figsize=(16,16),dpi=300)  # Initialize a canvas

() is to map directly on top of the currently active axes.

Knowing these two basics, let's look at subplot and subplots

fig = (figsize=(12, 4), dpi=200)
for i in range(len(img)):
    (1, len(img),i+1)
    (img[i])
()

The function is to specify the location of the subgraph, for example, there is now a total of 1 row and 10 columns, where the current subgraph is located;

When using this function you need to define a large drawing first, because the subplot function can not change the size of the drawing and resolution and other information; so you must pass fig = (figsize=(12, 4), dpi=200) to define the drawing-related settings;

Meanwhile, a convenient follow-up to this function is to use plt directly, to get the current active layer

fig, ax = (1, len(img), figsize=(15, 10))
for i in range(len(img)):
    ax[i].imshow(img[i])
()

When using a function, you can set the subdrawing information directly inside that function

This function returns two variables, a Figure instance, fig, and an AxesSubplot instance, ax. fig represents the entire image, and ax represents the axes and subplots drawn, with subscripts to get the desired subregions.

When we need to manipulate the subgraphs later, we can just ax[i].imshow(img[i]) directly

The difference with subplot is that (1) there is no need to manipulate layers via plt, each layer has specified axes; (2) one is written outside of the for loop and one is written inside; it boils down to the reason or how many plots are plotted by suplots is already specified, so the axes are already prepared ahead of time, whereas the subplot function is plotted once when it's called and there is no specified

subplot and subplots can be achieved by drawing subplot function, except that subplots help us plan the board, return an array of coordinates object, while subplot can only return one coordinate object each time, subplots can also directly specify the size of the board.

Axis Modification

We often need to modify information such as axis size, scale, etc., whether it's a subplot or a big picture; here's a look at the difference between subplots and subplot in terms of modifying coordinates

plt in modify coordinates directly write (); (); (); (); () and so on on the line, but axes and plt is not the same, axes need to add the set, for example: axes.set_xlabel(); axes.set_xlim() This point need to be extra careful!

For modifying the axes information of subplots, it is obviously more convenient for subplots because he has separate axes and it is more convenient to make the axes different for each subplot, for example

fig, ax = (1, len(img), figsize=(15, 10))
for i in range(len(img)):
    ax[i].imshow(img[i])
    ax[i].set_xlabel("test csdn")
# If you want to modify the axes individually
ax[5].set_xlabel("test csdn")
()

In case of subplot modification, the reference code is as follows:

fig = (figsize=(12, 4), dpi=200)
for i in range(len(img)):
    (1, len(img),i+1)
    (img[i])
    ("csdn test")
()

Of course, subplot can also have a return value, but this return value is one by one, not a bunch of the same as subplots are given to you

fig = (figsize=(12, 4), dpi=200)
for i in range(len(img)):
    ax=(1, len(img),i+1)
    #Or so #
    # ax = () Get the currently active sublayer
    (img[i])
    ax.set_xlabel("csdn test")  # Actually, it's still not possible to modify individual subgraphs.
()

xlabel, ylabel: set the label and size of horizontal and vertical axes

 for example("csdn test", fontsize=20)

xlim, ylim: set the starting point of the x and y axes (from where to where)

 for example(0,50) (0,60)

ticks, yticks: set the font size of the axis scale

 for example(labelsize=20)

title: set the title of the image

 for example(“csdn test”)

summarize

to this article on python plotting skills of subgraphs, and axis modification of the article is introduced to this, more related python and axis modification of the contents of the search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!