First, the basic drawing library: Matplotlib.pyplot
1. Simple drawing: Two curves
1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4 if __name__=='__main__':5X1 = Np.linspace (-3, 3, 15)6y1 = [d**2 forDinchX1]7 #Drawing8 plt.plot (x1, y1)9x2 = []Teny2 = [] One forI, datainchEnumerate (y1): A ifData >= 4: -x =X1[i] -y =Data theText ="(%d,%d)"%(x, y) - #add a callout to the image, where the index is actually the x-coordinate, data is the y-coordinate, and text is the displayed textual content -Plt.text (x, y, text, color ='Green') - x2.append (x) + y2.append (y) -Plt.plot (x2, Y2,'o', color='Green') +Plt.show ()
View Code
The drawing is as follows:
- Add callout to Image: Plt.text (x, y, text, color = ' green ')
- Specifies the format of the drawing when drawing, by default is a straight line, ' O ' indicates the use of dots: Plt.plot (x2, y2, ' o ', color= ' green ')
2, specify the x-axis as the date of the drawing:
1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4 if __name__=='__main__':5X1 = np.linspace (1, 15, 15)6y1 = [(X**2/40 + x* (0.3**x) + x/20) forXinchX1]7 #Drawing8Fig, ax =plt.subplots ()9 ax.plot (x1, y1)Tenx2 = [] Oney2 = [] A forI, datainchEnumerate (y1): - ifI% 3 = =0: -x =X1[i] they =Data -Text ="(%d,%f)"%(x, y) - #add a callout to the image, where the index is actually the x-coordinate, data is the y-coordinate, and text is the displayed textual content -Plt.text (x, y, text, color ='Green') + x2.append (x) - y2.append (y) +Plt.plot (x2, Y2,'o', color='Green') A at #Specify the name of the x-axis and y-axis -Plt.xlabel ("Date") -Plt.ylabel ("value") - #xticks Specifies which locations on the axis to display the label -Xticks = X1[0::3] - #xticklabels Specifies the specific contents of the label, here is the date inXticklabels = ['2016-03-0'+str ((n-1) *3+1) forNinchRange (1,len (xticks) +1)] - ax.set_xticks (xticks) toAx.set_xticklabels (Xticklabels, rotation=15) +Plt.show ()Viewcode: Adding x-axis display text
The drawing is as follows:
How to set the display text content of the x-axis:
1 # xticks Specifies which locations on the axis to display the label 2 xticks = X1[0::3]3# xticklabels Specifies the specific contents of the label, here is the date 4 Xticklabels = ['2016-03-0' for in range (1,len (xticks) +1)] 5ax.set_xticks (xticks)6 ax.set_xticklabels (Xticklabels, rotation=15)
Python:matplotlib Drawing Primer