Home > Backend Development > Python Tutorial > Summary of using Python to draw charts

Summary of using Python to draw charts

高洛峰
Release: 2017-02-13 13:38:11
Original
3034 people have browsed it

This article mainly introduces a comprehensive summary of using Python to draw charts. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

Before using Python to draw charts, we need to install two library files, numpy and matplotlib.

Numpy is an open source numerical computing extension for Python, which can be used to store and process large matrices and is more efficient than Python's own data structure; matplotlib is a Python image framework, using its graphic effects and drawing under MATLAB The graphics are similar.

Below I will introduce how to use Python to draw through some simple code.

1. Graphic drawing

Summary of using Python to draw charts

##Histogram

importmatplotlib.pyplotasplt

importnumpyasnp

mu=100

sigma=20

x=mu+sigma*np.random.randn(20000)# 样本数量

plt.hist(x,bins=100,color='green',normed=True)# bins显示有几个直方,normed是否对数据进行标准化

plt.show()
Copy after login

Bar chart

importmatplotlib.pyplotasplt

importnumpyasnp

y=[20,10,30,25,15]

index=np.arange(5)

plt.bar(left=index,height=y,color='green',width=0.5)

plt.show()
Copy after login

Line chart

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.linspace(-10,10,100)

y=x**3

plt.plot(x,y,linestyle=&#39;--&#39;,color=&#39;green&#39;,marker=&#39;<&#39;)

plt.show()
Copy after login

scatterplot

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.random.randn(1000)

y=x+np.random.randn(1000)*0.5

plt.scatter(x,y,s=5,marker=&#39;<&#39;)# s表示面积,marker表示图形

plt.show()
Copy after login

pie chart

importmatplotlib.pyplotasplt

importnumpyasnp

labels=&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;

fracs=[15,30,45,10]

plt.axes(aspect=1)#使x y轴比例相同

explode=[0,0.05,0,0]# 突出某一部分区域

plt.pie(x=fracs,labels=labels,autopct=&#39;%.0f%%&#39;,explode=explode)#autopct显示百分比

plt.show()
Copy after login

boxplot

Mainly used to display the dispersion of data. The graph is divided into upper edge, upper quartile, median, lower quartile, and lower edge. The outside points are outliers

importmatplotlib.pyplotasplt

importnumpyasnp

np.random.seed(100)

data=np.random.normal(size=(1000,4),loc=0,scale=1)

labels=[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;]

plt.boxplot(data,labels=labels)

plt.show()
Copy after login

2. Image adjustment

1. 23 point shapes

"."point","pixel"o"circle"v"triangle_down

"^"triangle_up"<"triangle_left">"triangle_right"1"tri_down

"2"tri_up"3"tri_left"4"tri_right"8"octagon

"s"square"p"pentagon"*"star"h"hexagon1"H"hexagon2

"+"plus"x"x"D"diamond"d"thin_diamond
Copy after login

2. 8 built-in default color abbreviations

b:blueg:greenr:redc:cyan

m:magentay:yellowk:blackw:white
Copy after login

3. 4 types of linearity

- solid line--dashed line-.dash line: dotted line

4. Draw sub-pictures on one picture

Summary of using Python to draw charts

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.arange(1,100)

plt.subplot(221)#2行2列第1个图

plt.plot(x,x)

plt.subplot(222)

plt.plot(x,-x)

plt.subplot(223)

plt.plot(x,x*x)

plt.subplot(224)

plt.plot(x,np.log(x))

plt.show()
Copy after login

5. Generate grid

Summary of using Python to draw charts

importmatplotlib.pyplotasplt

importnumpyasnp

y=np.arange(1,5)

plt.plot(y,y*2)

plt.grid(True,color=&#39;g&#39;,linestyle=&#39;--&#39;,linewidth=&#39;1&#39;)

plt.show()
Copy after login

6. Generate legend

Summary of using Python to draw charts

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.arange(1,11,1)

plt.plot(x,x*2)

plt.plot(x,x*3)

plt.plot(x,x*4)

plt.legend([&#39;Normal&#39;,&#39;Fast&#39;,&#39;Faster&#39;])

plt.show()
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study , I also hope that everyone will support the PHP Chinese website.

For more articles related to the summary of drawing charts using Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template