Python-matplotlib Learning (1)

Source: Internet
Author: User

1 Import Matplotlib.pyplot as Plt 2 Import NumPy as NP 3 4 x=np.linspace ( -1,1,50)5 y=2*x+16plt.plot (x, y)7 Plt.show ()

2: draw an image of Y=x*x

1 Import Matplotlib.pyplot as Plt 2 Import NumPy as NP 3 4 x=np.linspace ( -1,1,50)5#y=2*x+16 y=x**27  plt.plot (x, y)8 plt.show ()

3:figure's Understanding

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4X=np.linspace ( -1,1,50)5Y1=2*x+16Y2=x**27 8Plt.figure ("First figure")9 Plt.plot (x,y1)Ten  onePlt.figure ()#look at the default Name. a Plt.plot (x,y2) -Plt.plot (x,y1,color='Red', linewidth=2.0,linestyle='--')#draw two lines color red, width 2, dashed line -  the  -Plt.show ()

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4X=np.linspace ( -3,3,50)5Y1=2*x+16Y2=x**27 8Plt.figure ()#look at the default Name.9 Plt.plot (x,y2)TenPlt.plot (x,y1,color='Red', linewidth=2.0,linestyle='--')#draw two lines color red, width 2, dashed line onePlt.xlim ( -1,2)#X-axis Value range aPlt.ylim ( -2,3)#Y-axis Value Range -  -Plt.xlabel ("X-axis") thePlt.ylabel ("Y-axis")#Description -  -Plt.show ()
View Code

Solve the problem of Chinese garbled appearance

Locate the Matplotlibrc file under the Matplot installation path open and modify with the text Editing tool
#font. Family:sans-serif Remove #

#font. Sans-serif this line to Dot # and add Microsoft Ya-black font

This is the solution I found on the internet. I tried the Egg.

For the time being, wait till you find a better way.

Replace axis measures

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4X=np.linspace ( -3,3,50)5Y1=2*x+16Y2=x**27 8Plt.figure ("figure can be in Chinese")#look at the default Name.9 Plt.plot (x,y2)TenPlt.plot (x,y1,color='Red', linewidth=2.0,linestyle='--')#draw two lines color red, width 2, dashed line one  aPlt.xlim ( -1,2)#X-axis Value range -Plt.ylim ( -2,3)#Y-axis Value Range -  the  -Plt.xlabel ("I am x") -Plt.ylabel ("I am y")#Description - #plt.legend (prop=font) +  -New_ticks=np.linspace ( -1,2,5) + Print(new_ticks) a plt.xticks (new_ticks) atPlt.yticks ([ -2,-1.8,-1,1.22,3],[r"$really \ bad$"R"$bad $","$normal $","$good $","$really \ good$"]) -  -Plt.show ()

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4X=np.linspace ( -3,3,50)5Y1=2*x+16Y2=x**27 8Plt.figure ("figure can be in Chinese")#look at the default Name.9 Plt.plot (x,y2)TenPlt.plot (x,y1,color='Red', linewidth=2.0,linestyle='--')#draw two lines color red, width 2, dashed line one  aPlt.xlim ( -1,2)#X-axis Value range -Plt.ylim ( -2,3)#Y-axis Value Range -  the  -Plt.xlabel ("I am x") -Plt.ylabel ("I am y")#Description - #plt.legend (prop=font) +  -New_ticks=np.linspace ( -1,2,5) + Print(new_ticks) a plt.xticks (new_ticks) atPlt.yticks ([ -2,-1.8,-1,1.22,3],[r"$really \ bad$"R"$bad $","$normal $","$good $","$really \ good$"]) -  - #GCA = ' Get current axis ' -  -ax=PLT.GCA () -ax.spines[' right'].set_color ('None')#Right axis hidden inax.spines['Top'].set_color ('None')#Upper side Axis hidden -Ax.xaxis.set_ticks_position ('Bottom') toAx.yaxis.set_ticks_position (' left') +ax.spines["Bottom"].set_position (('Data', 0))#Set X-axis position -ax.spines[" left"].set_position (('Data', 0)) the  *Plt.show ()
View Code

1 ImportMatplotlib.pyplot as Plt2 ImportNumPy as NP3 4X=np.linspace ( -5,5,100)5Y1=2*x+16Y2=x**27 8Plt.xlim ( -5,5)#X-axis Value range9Plt.ylim ( -5,25)#Y-axis Value RangeTenPlt.xlabel ("I am x") onePlt.ylabel ("I am y")#Description a  -L1,=plt.plot (x,y1,label="y=2x+1") -L2,=plt.plot (x,y2,color='Red', linewidth=1.0,linestyle='--', label="y=x*x")#comma is required thePlt.legend (handles=[l1,l2,],labels=["AAA","BBB"],loc=' best') -  -Plt.show ()
View Code

Python-matplotlib Learning (1)

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.