Python
There are a lot of , beyond count , Each have advantages and disadvantages . In this paper, we will focus on pylab
To introduce . The reason for introducing this model is , It's because of it and Matlab
Strong similarity of , If you have Matlab
, So believe it pylab
You're going to get started soon .
pylab
mapping , The most basic function is plot
function , Of course, if you want to show the picture , You need to add an extra show
function .
stay python
In the drawing of ,numpy
Is a very common tool , If you are not familiar with it, you can refer to another blog post of the blogger :【Python】Python And Numpy The super practical foundation of the detailed tutorial .
for example :
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y = [i*i for i in np.arange(0, 1, 0.05)]
pylab.plot(x, y)
pylab.show()
Run the generated image as :
Specify two sequences , You just need two sequences of equal length , You can take one of these sequences as the abscissa , It's a sequence of zeros , Drawing .
But you can see , Such pictures are rather shabby , simple . We can make some modifications to the lines , For example, line type 、 Color 、 Dot pattern and so on . Only need plot
Add a parameter to the function . The usage of this parameter is flexible , You can choose from the values in the table below :
Color | Linetype | Point type |
---|---|---|
‘b’ ( Blue ) | ‘-’ ( Solid line ) | ‘,’ ( Pixels ) |
‘g’ ( green ) | ‘–’ ( Dotted line ) | ‘o’ ( circular ) |
‘r’ ( Red ) | ‘-.’ ( Dotted line ) | ‘^’ ( Upper triangle ) |
‘y’ ( yellow ) | ‘:’ ( Dotted line ) | ‘s’ ( square ) |
‘k’ ( black ) | ‘.’ ( spot ) | ‘+’ ( plus ) |
‘w’ ( Blue ) | … | ‘x’ ( Fork ) |
… | … |
for example :
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y = [i*i for i in np.arange(0, 1, 0.05)]
pylab.plot(x, y, "b-.+")
pylab.show()
Run the generated image as :
If , Also want to add a legend ,x
Axis 、y
The meaning and scale of the axis , The information of the title , You can also add functions .
for example :
# -*- coding:UTF-8 -*-
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y = [i*i for i in np.arange(0, 1, 0.05)]
pylab.plot(x, y, "b-.+", label='line')
pylab.xlabel('x') # x、y The introduction of the shaft
pylab.ylabel('y')
pylab.xlim([0, 1]) # x、y The length range of the axis
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8) # x、y Axis scale
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y') # x、y The title of the
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0)) # The location of the legend
pylab.show()
Run the generated image as :
It's not that I feel enriched in a moment !
Of course , Many times you need to draw multiple broken lines . Obviously , You can choose to draw multiple lines on the same image , You can also choose to draw different subgraphs on the image .
Draw multiple broken lines on the same image , It's very simple , Directly again plot
A straight line will do .
for example :
# -*- coding:UTF-8 -*-
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y1 = [i*i for i in np.arange(0, 1, 0.05)]
y2 = [i+i for i in np.arange(0, 1, 0.05)]
pylab.plot(x, y1, "b-.+", label='line1')
pylab.plot(x, y2, "r-.+", label='line2')
pylab.xlabel('x')
pylab.ylabel('y')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.show()
Run the generated image as :
Draw multiple broken lines on different subgraphs of the image , This needs to go through subplot
Divide and specify regions .
subplot(numRows, numCols, plotNum)
This function divides the image into numRows That's ok 、nulCols Column , And then follow from left to right 、 Number from top to bottom , The number on the top left is 1.plotNum Parameter specifies the region of the subgraph .
for example :
# -*- coding:UTF-8 -*-
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y1 = [i*i for i in np.arange(0, 1, 0.05)]
y2 = [i+i for i in np.arange(0, 1, 0.05)]
pylab.subplot(1, 2, 1)
pylab.plot(x, y1, "b-.+", label='line1')
pylab.xlabel('x')
pylab.ylabel('y1')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y1')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.subplot(1, 2, 2)
pylab.plot(x, y2, "r-.+", label='line2')
pylab.xlabel('x')
pylab.ylabel('y2')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y2')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.show()
Run the generated image as :
This is a more regular example , If it is irregular ? for example , The first line is two pictures , The second line is a picture . It's going to be a little bit more flexible .
The first line is actually based on 2*2
The first and second of the division , The second line is actually based on 2*1
The second line of the division . In this way, thinking will be solved easily .
# -*- coding:UTF-8 -*-
import pylab
import numpy as np
if __name__ == "__main__":
x = np.arange(0, 1, 0.05)
y1 = [i*i for i in np.arange(0, 1, 0.05)]
y2 = [i+i for i in np.arange(0, 1, 0.05)]
pylab.subplot(2, 2, 1)
pylab.plot(x, y1, "b-.+", label='line1')
pylab.xlabel('x')
pylab.ylabel('y1')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y1')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.subplot(2, 2, 2)
pylab.plot(x, y2, "r-.+", label='line2')
pylab.xlabel('x')
pylab.ylabel('y2')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y2')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.subplot(2, 1, 2)
pylab.plot(x, y1, "b-.+", label='line1')
pylab.plot(x, y2, "r-.+", label='line2')
pylab.xlabel('x')
pylab.ylabel('y')
pylab.xlim([0, 1])
pylab.ylim([0, 1])
pylab.xticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.yticks(np.arange(0, 1, 0.05), fontsize=8)
pylab.title('x-y')
pylab.legend(loc=3, borderaxespad=0., bbox_to_anchor=(0, 0))
pylab.show()
Run the generated image as :
In addition to the above functions , There are other common functions .
pylab.grid() # Grid drawing
pylab.savefig( Image storage path , dpi=200) # Save as picture
Master these basic discount graph drawing functions , I believe that the general line chart can be easily mastered .