Using Matplotlib simple Drawing

Source: Internet
Author: User
Tags cos ggplot
each component name in a picture

Draw multiple pictures

Import NumPy as NP
import Matplotlib.pyplot as Plt
plt.style.use ("Ggplot") # display style
def f (t):
    return Np.exp (-T) *np.cos (2*np.pi*t)
t1 = Np.arrange (0.0,5.0,0.1)
t2 = Np.arrange (0.0,5.0,0.02)

plt.figure (1) # Create a figure
Plt.subplot (211) # Create a 2-row 1-column diagram on the figure, and currently select 1th Figure
# Draw (t1,f (T1)) and (T2,f (T2)), one in a curved form, one in the form of a point.
Plt.plot (t1,f (T1), ' Bo ', T2,f (T2), ' K ') 

Plt.subplot (212) # Continue to select the second figure in the 2 row 1 column diagram
plt.plot (t2,np.cos NP.PI*T2), ' r--') # on the second figure shows the figure plt.show of Cos
()

The results are shown in the following illustration:

Set Axis information

Import NumPy as NP
import Matplotlib.pyplot as Plt
plt.style.use ("Ggplot")
np.random.seed (19680801)
Mu,sigma = 100,15
x = MU+SIGMA*NP.RANDOM.RANDN (10000) #生成10000服从正态分布的数
# Displays a histogram, divided into 50 groups,
n,bins,patches = Plt.hist (x,50,normed =1, Facecolor = ' g ', alpha=0.75)
# Set property information for x, y axes, information, font size, and color
plt.xlabel (' Smarts ', FontSize =14, color = ' red ')
Plt.ylabel ("probability", FontSize =, color = ' blue ')
plt.title ("Histogram of IQ" # Show Graph title
# Displays Latex formula at x = 60,y = 0.025
plt.text (. 025, R ' $\mu=100,\ \sigma=15$ ') # 
Plt.axis ([ 40,160,0,0.03]) # Set axis range, x range 40~160,y range 0~0.03
Plt.grid (True) # Show Squares
plt.tight_layout () # Eliminate the blank space around the picture
# Set the size of the scale
plt.xticks (fontsize =)
plt.yticks (fontsize =)

plt.show ()

The results are shown in the following illustration:


Text for more property information.
Text Information display mathematical formula

Set Display style

Import NumPy as NP
import Matplotlib.pyplot as Plt
plt.style.use (' Ggplot ') # Set the style of the R language Pack
# View available display styles, The style is as follows: You can try different styles yourself
print (plt.style.available) ""
[' Seaborn-darkgrid ', ' Seaborn-dark-palette ', ' Seaborn-deep ', ' seaborn-colorblind ', ' seaborn-bright ', ' Seaborn-whitegrid ', ' seaborn-poster ', ' seaborn-talk ', ' Seaborn-ticks ', ' Seaborn-pastel ', ' dark_background ', ' ggplot ', ' Seaborn-dark ', ' bmh ', ' seaborn-white ', ' grayscale ', ' Classic ', ' seaborn-muted ', ' seaborn-paper ', ' seaborn-notebook ', ' FiveThirtyEight ' '
set legend
Import Matplotlib.pyplot as Plt
import numpy as NP
plt.style.use ("FiveThirtyEight") # set style
x = Np.linspace ( 0,2,100)
Plt.plot (x, X, label= ' linear ') # label to display legend
plt.plot (x, x**2, label= ' quadratic ') # label to display legend
Plt.plot (x, x**3, label= ' Cubic ') # label is used to display legend
Plt.xlabel (' x label ') Plt.ylabel (
' y label ') plt.title (
" Simple plot ")
plt.legend () # Displays legend
plt.tight_layout () # Compact Layout
# Sets the display position of the legend and the font size
plt.legend (loc = ' Upper Right ', FontSize =)
plt.show ()

The results are shown in the following illustration:


display Chinese characters correctly

Import Matplotlib.pyplot as Plt
plt.rcparams[' font.sans-serif ']=[' Simhei '] #用来正常显示中文标签

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.