Three basic drawing skills necessary for in-depth learning

Posted by jpbox on Wed, 10 Nov 2021 03:58:29 +0100

In depth learning, there are several commonly used diagrams:

  • diagram
  • Dot graph
  • display picture

1 installation

They are usually self-contained, such as conda software. If not, what about desktop installation? If it is already installed, you can skip it.

1. Open the console (win+R) - > Enter cmd
2. Enter the following command

pip install matplotlib

perhaps

conda install matplotlib

Just finish.

1.1 display Chinese

import matplotlib.pyplot as plt 
# Display Chinese 
plt.rcParams['font.sans-serif'] = [u'SimHei'] 
plt.rcParams['axes.unicode_minus'] = False

Here are their basic usage methods.

2 curve

2.1 basic use of curves

By drawing the curve of cos function

import matplotlib.pyplot as plt  
import numpy as np

def f(t):  
    return np.exp(-t) * np.cos(2 * np.pi * t)  
  
t1 = np.arange(0.0, 3.0, 0.01)
plt.plot(t1,f(t1))  
  
plt.show()

The effects are as follows:

2.2 basic properties of figure

attributepurpose
xlabelSet x to x
ylabelSet Y to y
ylim(m,n)Set y to be between m-n
xlim(m,n)Set x to range between m-n
titleSets the title of the icon

The specific codes are as follows:

import matplotlib.pyplot as plt  
import numpy as np  

def f(t):  
    return np.exp(-t) * np.cos(2 * np.pi * t)  

t1 = np.arange(0.0, 3.0, 0.01)  

plt.plot(t1, f(t1))  
  
plt.xlabel("x")  #Set x to x
plt.ylabel("y")  #Set Y to y
plt.ylim(0, 0.6)  #Set y between 0-0.6
plt.xlim((0, 2))  #Set x to range from 0-2
 
plt.show()

The effects are as follows:

2.3 plot attribute setting

attributepurposeSystem default
labelLabel linesnothing
colorSets the color of the lineg stands for green, b for blue, etc
import matplotlib.pyplot as plt  
import numpy as np  
  
  
def f(t):  
    return np.exp(-t) * np.cos(2 * np.pi * t)  

t1 = np.arange(0.0, 3.0, 0.01)   
  
plt.plot(t1, f(t1), label="cos", color="g")  
plt.plot(t1, t1 ** 2, label="quadratic", color="b")  

plt.show()

Green and blue are set for the two curves respectively, and the effects are as follows:

2.4 legend usage

When you need to mark and explain the diagrams with different colors when drawing multiple diagrams, you can display them with legends.
Set the label and color for each curve, and then set the legend() function.

2.5 specific use of subplot

In the actual project, you may want to compare various types of drawings. Can you draw multiple drawings on one canvas? It can be solved by subplot.

There are two methods commonly used in subplot. First, we will introduce the methods that are not easy to understand. (from my point of view).

2.5.1 subplot(pos, kwargs)

This pos consists of rows, columns and indexes. What do you mean?

The corresponding position of the index is from left to right and from top to bottom. Take 2 rows and 2 columns as an example to briefly explain the index:

Column 1Column 2
Line 112
Line 234

subplot(221) indicates that the graph has 4 (2 * 2) sub graphs, which are placed in the first one. The specific codes are as follows:

import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)

axs1 = plt.subplot(221)  #first
axs1.hist(data[0])  
  
axs2 = plt.subplot(222)  #the second
axs2.scatter(data[0], data[1]);  
  
axs3 = plt.subplot(223)  #Third
axs3.plot(data[0], data[1]);  
  
axs4 = plt.subplot(224)  #Fourth
axs4.hist2d(data[0], data[1]);  
  
plt.show()

If there are only three pictures, spread out at the bottom, desktop processing, in fact, you can think so. Divide the canvas into the upper half and the lower half. The upper half displays two and the lower half displays one.

import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  

axs1 = plt.subplot(221)  #Two rows and two columns, first.
axs1.hist(data[0])  
  
axs2 = plt.subplot(222)  #Two rows and two columns, in the second.
axs2.scatter(data[0], data[1]);  
  
axs3 = plt.subplot(212)  
axs3.plot(data[0], data[1]);  #Two rows and one column, on the second row

plt.show()

The effects are as follows:

2.5.2 subplot(nrows, ncols, index, **kwargs)

This method is actually easier and more intuitive. nrows represents rows, ncols represents columns, and index represents where to put them. It is essentially the same as the subplot(position) above.

  • First, implement a 2 * 2 Icon
import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  
  
axs1 = plt.subplot(2, 2, 1)  
axs1.hist(data[0])  
  
axs2 = plt.subplot(2, 2, 2)  
axs2.scatter(data[0], data[1], color="b");  
  
axs3 = plt.subplot(2,2,3)  
axs3.plot(data[0], data[1]);  

axs4 = plt.subplot(2,2,4)  
axs4.hist2d(data[0], data[1]);  
  
plt.show()

The effect is the same

  • Secondly, implement a and Figure_subplot_2 the same figure.
import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  

axs1 = plt.subplot(2,2,1)  #Two rows and two columns, first.
axs1.hist(data[0])  
  
axs2 = plt.subplot(2,2,2)  #Two rows and two columns, in the second.
axs2.scatter(data[0], data[1]);  
  
axs3 = plt.subplot(2,1,2)  
axs3.plot(data[0], data[1]);  #Two rows and one column, on the second row

plt.show()

2.6 annotation

Annotation is realized through annotate, which mainly consists of three parameters:

fieldsignificance
sContents of notes
xyPosition of arrow
xytextNote content location
arrowpropsStyle of arrow

The specific code is as follows:

x = np.arange(-10, 11, 1)  
y = x * x  
plt.title('title')  
plt.plot(x, y)  
# Add comments  
plt.annotate('this is annotate', xy=(0, 1), xytext=(-2, 22), arrowprops={'headwidth': 10, 'facecolor': 'r'})  
plt.show()

The effects are as follows:

3 dot chart

First look at the renderings:

The code is as follows:

import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  
  
plt.scatter(data[0], data[1],c="b",marker='o')  
  
plt.show()

What parameters are included in scatter?

3.1 parameter description

parameternameusage method
xx position of pointIt can be a fixed value or a list
yy position of pointIt can be a fixed value or a list
sSet the size of the pointIs a fixed size
ccolourIt can be an rgb value (#782728) or a color character (b)
markerShape of pointSee the table below
alphatransparencyBetween 0-1
linewidthsSets the width of the borderValues greater than 0

What are the markers? You can find them in matplotlib.markers.MarkerStyle.markers. I listed them. See the table below:

marker valueshape
'o'origin
'v'Lower triangle
'^'Upper triangle
'<'Left triangle
'>'Right triangle
'8'Octagonal
's'square
'p'pentagon
'*'star
'h'Hexagon 1
'H'Hexagon 2
'D'diamond
'd'Single rhomboid
'P'Filled plus sign
'X'Filled multiplier

3.2 specific implementation

Specific examples:

import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  
  
plt.scatter(data[0], data[1], s=data[1] * 20, c='b', marker='o', alpha=0.3, linewidths=2)  
  
plt.show()

design sketch:

import matplotlib.pyplot as plt  
import numpy as np  
  
np.random.seed(19680801)  
data = np.random.randn(2, 100)  
  
plt.scatter(data[0], data[1], s=data[1] * 20, c='r', marker='^', alpha=0.9, linewidths=1)  
  
plt.show()

design sketch:

4 display picture

import matplotlib.pyplot as plt  
  
image_content = plt.imread('myworkspace/pandasDemo/Figure_subplot.png')  #Read picture information
plt.imshow(image_content)  #display picture
plt.show()

Topics: Python AI Deep Learning