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

Summary of using Python to draw charts

Feb 13, 2017 pm 01:38 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles