matplotlib draws a row and three columns graph (usually used as a document picture)

Use matplotlib to draw a parallel line graph that often appears in the literature.

Attached code, and will explain some difficult points in drawing.

catalogue

design sketch

Drawing code:

1 data tuple

2. Subplot position: subplot setting

3. Line color and legend

4. tight_layout: fill in blanks and modify spacing

design sketch

The renderings are as follows:

Drawing code:

The code is as follows:

from matplotlib import pyplot as plt

x=range(0,4)


y_black=([4,5,16,18],[73,75,79,79],[11.5,12.3,15,14.7])
y_red=([18,17,23,25],[67,69,71,74],[13,13.8,15,14])
y_blue=([7,6,17,24],[70,70.5,77,79],[11.8,12.3,15,14.7])

figure=plt.figure(figsize=(11,3),dpi=80)
x_label=[13,32,37,103]

# Sub Figure 1
ax1=plt.subplot(131)
line_black, =plt.plot(x,y_black[0],linestyle='-',marker='s',color="black")
line_red, =plt.plot(x,y_red[0],linestyle=':',marker='o',color="red")
line_blue, =plt.plot(x,y_blue[0],linestyle='-.',marker='^',color="blue")
plt.xticks(x,x_label)
plt.yticks(list(range(5,31))[::5])
plt.xlabel("Parameter size(kb)")


# Sub Figure 2
ax2=plt.subplot(132)
line_black, =plt.plot(x,y_black[1],linestyle='-',marker='s',color="black")
line_red, =plt.plot(x,y_red[1],linestyle=':',marker='o',color="red")
line_blue, =plt.plot(x,y_blue[1],linestyle='-.',marker='^',color="blue")
plt.xticks(x,x_label)
ax2.set_xlabel("Parameter size(kb)")
plt.yticks([70,75,80])

plt.ylabel("Accuracy")


# Sub Figure 3
ax3=plt.subplot(133)
line_black, =plt.plot(x,y_black[2],linestyle='-',marker='s',color="black")
line_red, =plt.plot(x,y_red[2],linestyle=':',marker='o',color="red")
line_blue, =plt.plot(x,y_blue[2],linestyle='-.',marker='^',color="blue")
plt.xticks(x,x_label)
plt.xlabel("Parameter size(kb)")
plt.ylabel("Delay(s)")
plt.yticks(list(range(12,15))[::2])

figure.legend([line_black,line_red,line_blue],['A','B','C'],ncol=3,loc="upper center")

# plt.savefig("./t9.png")
plt.tight_layout()
plt.show()

1 data tuple

It can be found from the rendering that the accuracy of the A/B/C three lines under three different modes is to be drawn, and the scale value of x is the same, which means that the three figures can share the same value of x, but the value of y cannot.

Therefore, three tuples are established according to the line color of the y value, and each tuple has three lists corresponding to three graphs.

x=range(0,4)


y_black=([4,5,16,18],[73,75,79,79],[11.5,12.3,15,14.7])
y_red=([18,17,23,25],[67,69,71,74],[13,13.8,15,14])
y_blue=([7,6,17,24],[70,70.5,77,79],[11.8,12.3,15,14.7])

figure=plt.figure(figsize=(11,3),dpi=80)
x_label=[13,32,37,103]

2. Subplot position: subplot setting

Since the three graphs are juxtaposed, the subplot function in pyplot is used

Where, subplot(x,y,z),x represents the number of rows, Y represents the number of columns, and Z represents the number of regions in the graph.

# Sub Figure 1
ax1=plt.subplot(131)

# Sub Figure 2
ax2=plt.subplot(132)

# Sub Figure 3
ax3=plt.subplot(133)

3. Line color and legend

For line color, first define the line style, marker, and color attributes of plot, which are line form, dot mark, and color (which form corresponds to which letter can be found on the Internet). At the same time, the picture needs to be introduced by legend, so the lines drawn for the three types are set to line_black,line_red,line_blue is convenient for legend display.

The following code takes sub Figure 1 as an example:

line_black, =plt.plot(x,y_black[0],linestyle='-',marker='s',color="black")
line_red, =plt.plot(x,y_red[0],linestyle=':',marker='o',color="red")
line_blue, =plt.plot(x,y_blue[0],linestyle='-.',marker='^',color="blue")

The legend shows a line of code:

figure.legend([line_black,line_red,line_blue],['A','B','C'],ncol=3,loc="upper center")

4. tight_layout: fill in blanks and modify spacing

At the beginning, no tight was added_ The layout() function will show that the label on the x-axis is too far from the image, and it appears to be very empty, as shown in the following figure.

If the figure is small, the label will not be displayed.  

After being displayed, the picture is a little distorted.

Add plt.tight before plt.show_ Layout().

plt.tight_layout()

Tags: Python Pycharm matplotlib

Posted by dearsina on Wed, 24 Aug 2022 10:39:26 +0300