Matplotlib is python’s most famous drawing library, which provides a full set of command apis similar to Matlab, perfect for interactive drawing.
It’s fairly well documented, and the Gallery page has hundreds of thumbnails, all of which are active when opened. So if you need to draw a certain type of diagram, just browse/copy/paste it on this page and it’s pretty much done.
This is the rendering to be implemented, using Matplotlib to plot y = x^2^.
Code implementation
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
# set x, y range and label
plt.xlim(- 1.2)
plt.ylim(2 -.3)
plt.xlabel('x')
plt.ylabel('y')
# Set the axis scale
# Tick X range (-1, 1) Tick Label(-1, -0.25, 0.5, 1.25, 2
new_ticks=np.linspace(- 1.2.5)
plt.xticks(new_ticks)
# Tick Y range (,1,1.5-2.2-1, 2.4), Tick Label (2.2, 1, 1, 1.5, 2.4) alias (in English) below
plt.yticks([2.2.- 1.1.1.5.2.4],
[r'$really\ bad$'.r'$bad$'.r'$normal$'.r'$good$'.r'$really\ good$'])
Gca () gets axis information
ax=plt.gca()
# use.spines to set the border: x; Set the color on the right to None.
Set_position: y=0; (All attributes of location: Outward, axes, data)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# move the axes
Set the bottom x axis to y=0.
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data'.0))
Set the left (y) axis to x=0.
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data'.0))
# set the tag
ax.set_title('y = x^2',fontsize=14,color='r')
# display image
plt.show()
Copy the code
First, an image of the components of Matplotlib is presented.
Basic composition
In matplotlib, the entire image is a Figure object. A Figure object can contain one or more Axes objects. Each Axes(AX) object is a plot area with its own coordinate system.
The ownership relationship is as follows (picture from network) :
On the figure: Title is the image Title, Axis is the coordinate Axis, Label is the coordinate Axis Label, Tick is the scale line, Tick Label is the scale note.
Each object relationship can be sorted into the following content (pictures from the Internet) :
Matplotlib plotting step analysis
Pay attention to
I’m not going to install Matplotlib here. I’m using the Anaconda notebook, so I can import the Matplotlib module directly.
If you don’t want to use Anaconda, check out this recommended article on how to install Matplotlib
Although the whole code is posted above, it is easy to understand for those with matplotlib foundation, but for beginners, it is quite eye-catching.
Matplotlib simple image implementation
Import the module matplotlib.pyplot using import and shorten it to PLT using import and shorten it to NP using numpy.
# import module
import matplotlib.pyplot as plt
import numpy as np
Copy the code
Use Np.linspace to define x: the range is (-3, 3) and the number is 50. Simulate one-dimensional data group (x,y) to represent the curve.
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Copy the code
Define an image window using plt.figure, plot (x,y) curves using plt.plot, and display images using plt.show.
# Draw and display
plt.figure()
plt.plot(x, y)
plt.show()
Copy the code
The final code
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
plt.figure()
plt.plot(x, y)
plt.show()
Copy the code
The results
If you see this, it’s not very different from the final rendering, it doesn’t matter. Let’s move on.
2. Set the Figure image
Define an image window with plt.figure: number 3, size (8, 5). Plot (x,y) using plt.plot with the color attribute of the curve being red. The linewidth of the curve is 1.0. The type of curve (Linestyle) is dashed line, using plt.show to display the image.
plt.figure(num=3,figsize=(8.5))
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
Copy the code
The final code
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
plt.show()
Copy the code
Running effect
Three, set the axis
Set x and y coordinate range and label
Use plt.xlim to set the X-axis range :(-1, 2); Use plt.ylim to set the Y-axis range :(-2, 3);
Use plt.xlabel to set the x axis name: ‘x’; Use plt.ylabel to set the y axis name: ‘I am y’;
Code implementation
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
# set x, y range and label
plt.xlim(- 1.2)
plt.ylim(2 -.3)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Copy the code
The results
Set x, Y scale range and scale marking
To set the X-axis, use np.linspace to define the range and number: the range is (-1,2); The number is 5.
Set the xticks with plt.xticks: the range is (-1,2); The number is 5.
Set yticks to [-2, -1.8, -1, 1.22, 3] using plt.yticks. The corresponding scale is named [‘ really bad ‘, ‘bad’, ‘normal’, ‘good’, ‘really good’].
# Set the axis scale
# Tick X range (-1, 1) Tick Label(-1, -0.25, 0.5, 1.25, 2
new_ticks=np.linspace(- 1.2.5)
plt.xticks(new_ticks)
# Tick Y range (,1,1.5-2.2-1, 2.4), Tick Label (2.2, 1, 1, 1.5, 2.4) alias (in English) below
plt.yticks([2.2.- 1.1.1.5.2.4],
[r'$really\ bad$'.r'$bad$'.r'$normal$'.r'$good$'.r'$really\ good$'])
Copy the code
The final code
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
# set x, y range and label
plt.xlim(- 1.2)
plt.ylim(2 -.3)
plt.xlabel('x')
plt.ylabel('y')
# Set the axis scale
# Tick X range (-1, 1) Tick Label(-1, -0.25, 0.5, 1.25, 2
new_ticks=np.linspace(- 1.2.5)
plt.xticks(new_ticks)
# Tick Y range (,1,1.5-2.2-1, 2.4), Tick Label (2.2, 1, 1, 1.5, 2.4) alias (in English) below
plt.yticks([2.2.- 1.1.1.5.2.4],
[r'$really\ bad$'.r'$bad$'.r'$normal$'.r'$good$'.r'$really\ good$'])
# display image
plt.show()
Copy the code
Operation effect, pay attention to the x, y axis scale and marking changes
Move the axes
Hide the right border and top border
-
Use plt.gca to get the current coordinate information.
-
Use.spines to set the border: the right border; Use. Set_color to set the border color: white by default;
Set borders with.spines: top borders; Use. Set_color to set the border color: white by default;
Adjust axis to center (adjust left border to x=0 and bottom border to y=0)
- use
.xaxis.set_ticks_position
Set the position of the number or name on the X coordinate scale:bottom
. (All locations:top
.bottom
.both
.default
.none
). - use
.spines
Set border: x axis; use.set_position
Set border position: y=0 position; Position all attributes:outward
.axes
.data
)
Gca () gets axis information
ax=plt.gca()
# use.spines to set the border: x; Set the color on the right to None.
Set_position: y=0; (All attributes of location: Outward, axes, data)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# move the axes
Set the bottom x axis to y=0.
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data'.0))
Set the left (y) axis to x=0.
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data'.0))
Copy the code
The final code
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
# set x, y range and label
plt.xlim(- 1.2)
plt.ylim(2 -.3)
plt.xlabel('x')
plt.ylabel('y')
# Set the axis scale
# Tick X range (-1, 1) Tick Label(-1, -0.25, 0.5, 1.25, 2
new_ticks=np.linspace(- 1.2.5)
plt.xticks(new_ticks)
# Tick Y range (,1,1.5-2.2-1, 2.4), Tick Label (2.2, 1, 1, 1.5, 2.4) alias (in English) below
plt.yticks([2.2.- 1.1.1.5.2.4],
[r'$really\ bad$'.r'$bad$'.r'$normal$'.r'$good$'.r'$really\ good$'])
Gca () gets axis information
ax=plt.gca()
# use.spines to set the border: x; Set the color on the right to None.
Set_position: y=0; (All attributes of location: Outward, axes, data)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# move the axes
Set the bottom x axis to y=0.
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data'.0))
Set the left (y) axis to x=0.
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data'.0))
# display image
plt.show()
Copy the code
The results
5. Set the title
Set the title with.set_title and declare the title y = x^2^, font size 14, and color red.
# set the tag
ax.set_title('y = x^2',fontsize=14,color='r')
Copy the code
The final code
import matplotlib.pyplot as plt
import numpy as np
# Define the range (-3, 3) of the x variable in number 50
x=np.linspace(- 3.3.50)
y=x**2
Figure and specify the size
plt.figure(num=3,figsize=(8.5))
# Draw y=x^2 image, set color to red, line width to 1, line style to --
plt.plot(x,y,color='red',linewidth=1.0,linestyle=The '-')
# set x, y range and label
plt.xlim(- 1.2)
plt.ylim(2 -.3)
plt.xlabel('x')
plt.ylabel('y')
# Set the axis scale
# Tick X range (-1, 1) Tick Label(-1, -0.25, 0.5, 1.25, 2
new_ticks=np.linspace(- 1.2.5)
plt.xticks(new_ticks)
# Tick Y range (,1,1.5-2.2-1, 2.4), Tick Label (2.2, 1, 1, 1.5, 2.4) alias (in English) below
plt.yticks([2.2.- 1.1.1.5.2.4],
[r'$really\ bad$'.r'$bad$'.r'$normal$'.r'$good$'.r'$really\ good$'])
Gca () gets axis information
ax=plt.gca()
# use.spines to set the border: x; Set the color on the right to None.
Set_position: y=0; (All attributes of location: Outward, axes, data)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# move the axes
Set the bottom x axis to y=0.
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data'.0))
Set the left (y) axis to x=0.
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data'.0))
# set the tag
ax.set_title('y = x^2',fontsize=14,color='r')
# display image
plt.show()
Copy the code
The results
Finally, attach the overall structure of Matplotlib (image from network) :
Recommended reading
The day I came to Beijing
So many years of learning in vain, the original method is useless
Basri’s Journey through Python — In depth slicing operations and principles
Wall cracks recommend Anaconda | amway Python IDE
Half the world of programming
Good book recommendations
Statistical learning method
Recently, I am studying algorithms and formulas in machine learning. My friend recommended this book to me. After some investigation, I finally think this book is worthy of readers and deserves to be recommended.
Hang Li graduated from the Department of Electrical Engineering, Kyoto University, Japan, and received a PhD in computer science from the University of Tokyo, Japan. He once worked in NEC Central Research Institute of Japan, senior researcher and principal researcher of Microsoft Research Asia, and is now the chief scientist of Huawei Noah’s Ark Laboratory. Visiting professor at Peking University, Nankai University and Xi ‘an Jiaotong University. His research interests include information retrieval, natural language processing, statistical machine learning, and data mining.
Douban profile
Support vector machine, Boosting, maximum entropy, conditional random field and other statistical learning methods are introduced in detail.
Thinking power: efficient systems thinking
Wang Shimin is CEO of Erya, founder of YouCore, and author of zhihu column “The Power of Frames”.
A paradox. In the mind quite despise education, but accidentally read a famous school, took a master’s degree; Believe in personal freedom, but set up a company to tie oneself to death; I don’t really like teaching, but now I’m training. A restless person. In just ten years, I have been a helpless and painful IT code farmer, a consultant and now a private enterprise boss.
This book is very helpful to improve my thinking ability, and many learning methods provided in the book are worth learning from. Since reading this book, I also like to draw brain map summary and keep trying to learn systematically. I strongly encourage readers to check out the Power of Frames column to improve your thinking and learning.
Douban profile
“Really useful” is what makes this book different from other thinking books!
Have you ever been stuck in a situation where you can’t understand while analyzing, express yourself clearly, or learn quickly? Do you want to improve your thinking ability comprehensively? Do you know what problem solving skills and presentation skills are?
This book contains the answers you want to know.
The book is divided into 3 parts, a total of 10 chapters, each chapter can solve a problem in its own system, and the whole book also forms a set of systematic system thinking.
The first part explores the roots of the power of frames. The essence of everything is actually a system, and grasp the “framework” of the system will grasp the essence of things. There’s no problem that can’t be solved with a framework, and if one doesn’t work, two.
The second part breaks down the use of “framing” problem solving into five steps — defining the problem, framing the problem, identifying the key points, executing effectively, and reviewing the adjustments, and refining the corresponding good thinking methods and tools. He also taught the secret of quality problem solving with limited time and resources, hypothetically thinking.
The third part teaches how to use “frame” to express effectively. It demonstrates the charm of top-down expression layer by layer and illustrates the meaning of “pictorial expression” with rich diagrams.
For readers who want to improve their overall thinking ability, it is recommended to read the book in the order of the chapters; Readers who are eager to find specific problem-solving skills or presentation skills can also read the corresponding chapters directly.