Example of drawing with Python

Source: Internet
Author: User
1. Environment

System: WINDOWS10

Python version: python3.6.1

Libraries used: Matplotlib,numpy

Several methods of generating random numbers in 2.numpy library

Import NumPy as NP
Numpy.random
rand (D0, D1, ..., dn)

In [2]: X=np.random.rand (2,5)

In [3]: x
OUT[3]:
Array ([[0.84286554, 0.50007593, 0.66500549, 0.97387807, 0.03993009],
[0.46391661, 0.50717355, 0.21527461, 0.92692517, 0.2567891]])

Randn (D0, D1, ..., DN) query result as standard normal distribution

I n [4]: X=np.random.randn (2,5)

In [5]: x
Out[5]:
Array ([[-0.77195196, 0.26651203,-0.35045793,-0.0210377, 0.89749635],
[ -0.20229338, 1.44852833, -0.10858996, -1.65034606, -0.39793635]])

randint (lo w,high,size)

Generate low to high (half open interval [low, high), size data

In [6]: X=np.random.randint (1,8,4)

in [7 ]: x
Out[7]: Array ([4, 4, 2, 7])

random_integers (low,high,size)

Generate low to High ( Closed interval [Low, high), size data

In [ten]: X=np.random.random_integers (2,10,5)

In [all]: x
Out[11]: Array ([7, 4, 5 , 4, 2])

3. Scatter chart

x x axis y y axis s   dot Area C   color marker dot  shape alpha   dot transparency                #其他图也类似这种配置
n=50# Height=np.random.randint (150,180,20) # weight=np.random.randint (80,150,20) x=np.random.randn (N) y= Np.random.randn (N) plt.scatter (x,y,s=50,c= ' R ', marker= ' o ', alpha=0.5) plt.show ()

4. Line chart


x=np.linspace(-10000,10000,100) #将-10到10等区间分成100份y=x**2+x**3+x**7plt.plot(x,y)plt.show()

Line chart using the plot function

5. Bar chart

N=5y=[20,10,30,25,15]y1=np.random.randint (10,50,5) x=np.random.randint (10,1000,n) index=np.arange (N) Plt.bar (left =index,height=y,color= ' Red ', width=0.3) plt.bar (left=index+0.3,height=y1,color= ' black ', width=0.3) plt.show ()

Orientation setting a horizontal bar chart

N=5y=[20,10,30,25,15]y1=np.random.randint (10,50,5) x=np.random.randint (10,1000,n) Index=np.arange (N) # Plt.bar ( Left=index,height=y,color= ' Red ', width=0.3) # Plt.bar (left=index+0.3,height=y1,color= ' black ', width=0.3) #plt. Barh ( Plus h is the horizontal bar chart without setting Orientationplt.bar (left=0,bottom=index,width=y,color= ' red ', height=0.5,orientation= ' Horizontal ') plt.show ()

6. Histogram

M1=100sigma=20x=m1+sigma*np.random.randn (plt.hist) (x,bins=50,color= "green", Normed=true) Plt.show ()

# #双变量的直方图 # #颜色越深频率越高 # #研究双变量的联合分布
#双变量的直方图 # The darker the color the higher the frequency # study on the joint distribution of Bivariate X=np.random.rand (+2y=np.random.rand) +3plt.hist2d (x,y,bins=40) plt.show ()

7. Pie chart

#设置x, the y-axis ratio is 1:1, thus achieving a positive circle
#labels标签参数, X is the corresponding data list, autopct shows the proportion of each area, explode highlights a piece, Shadow Shadow
Labes=[' A ', ' B ', ' C ', ' D ']fracs=[15,30,45,10]explode=[0,0.1,0.05,0] #设置x, the y-axis ratio is 1:1, thus reaching a positive circle Plt.axes (aspect=1) # Labels tag parameter, x is the corresponding data list, autopct shows the proportion of each area, explode highlight a piece, Shadow Shadow Plt.pie (x=fracs,labels=labes,autopct= "%.0f%%", explode=explode,shadow=true) Plt.show ()

8. Box-type diagram

import Matplotlib.pyplot as Pltimport numpy as Npdata=np.random.normal (loc=0, scale=1,size=1000) The shape of the #sym point, whis the length of the dashed line Plt.boxplot (data,sym= "O", whis=1.5) plt.show () 
# Sym the shape of the point, whis the length of the dashed line 

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.