Python:matplotlib Drawing Primer

Source: Internet
Author: User

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:

    1. Add callout to Image: Plt.text (x, y, text, color = ' green ')
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.