Home Backend Development Python Tutorial Detailed explanation of examples of drawing graphics with python

Detailed explanation of examples of drawing graphics with python

Jun 20, 2017 pm 03:55 PM
python graphics draw

1. Environment

System: windows10

python version: python3.6.1

Libraries used: matplotlib, numpy

2. Several ways for the numpy library to generate random numbers

1

import numpy as np

Copy after login
numpy.random##random_integers(low,high,size)
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 is standard normal distribution

In [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.44 852833 , -0.10858996, -1.65034606, -0.39793635]])

randint(low,high,size)

Generate between low and 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])

Generate size data between low and high (closed interval [low, high))

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

In [11]: x

Out[11]: array([7, 4, 5, 4, 2])

3.Scatter chart

1

2

3

4

5

6

x x轴

y y轴

s   圆点面积

c   颜色

marker  圆点形状

alpha   圆点透明度                #其他图也类似这种配置

Copy after login

1

2

3

4

5

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()

Copy after login

##4. Line chart

1

2

3

4

<code class="python hljs"># 来源:百度网盘搜索 <br/>x=np.linspace(<span class="hljs-number">-10000,<span class="hljs-number">10000,<span class="hljs-number">100) <span class="hljs-comment">#将-10到10等区间分成100份

y=x**<span class="hljs-number">2+x**<span class="hljs-number">3+x**<span class="hljs-number">7

plt.plot(x,y)

plt.show()</span></span></span></span></span></span></span></code>

Copy after login

Line chart uses plot function

5. Bar chart

1

2

3

4

5

6

7

8

N=5

y=[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=&#39;red&#39;,width=0.3)

plt.bar(left=index+0.3,height=y1,color=&#39;black&#39;,width=0.3)

plt.show()

Copy after login

orientation set horizontal bar chart

1

2

3

4

5

6

7

N=5

y=[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=&#39;red&#39;,width=0.3)# plt.bar(left=index+0.3,height=y1,color=&#39;black&#39;,width=0.3)#plt.barh() 加了h就是横向的条形图,不用设置orientation

plt.bar(left=0,bottom=index,width=y,color=&#39;red&#39;,height=0.5,orientation=&#39;horizontal&#39;)

plt.show()

Copy after login

##6. Histogram

1

2

3

4

5

<strong>m1=100

sigma=20

x=m1+sigma*np.random.randn(2000)

plt.hist(x,bins=50,color="green",normed=True)

plt.show()</strong>

Copy after login

1

# #双变量的直方图# #颜色越深频率越高# #研究双变量的联合分布

Copy after login

1

2

3

4

5

#双变量的直方图#颜色越深频率越高#研究双变量的联合分布

x=np.random.rand(1000)+2

y=np.random.rand(1000)+3

plt.hist2d(x,y,bins=40)

plt.show()

Copy after login

7. Pie Chart

1

<strong>#设置x,y轴比例为1:1,从而达到一个正的圆</strong>

Copy after login

1

<strong>#labels标签参数,x是对应的数据列表,autopct显示每一个区域占的比例,explode突出显示某一块,shadow阴影</strong>

Copy after login

1

2

3

4

5

6

<strong>labes=[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;]

fracs=[15,30,45,10]

explode=[0,0.1,0.05,0]#设置x,y轴比例为1:1,从而达到一个正的圆

plt.axes(aspect=1)#labels标签参数,x是对应的数据列表,autopct显示每一个区域占的比例,explode突出显示某一块,shadow阴影

plt.pie(x=fracs,labels=labes,autopct="%.0f%%",explode=explode,shadow=True)

plt.show()</strong>

Copy after login

8. Box plot

1

<strong>import matplotlib.pyplot as pltimport numpy as npdata=np.random.normal(loc=0,scale=1,size=1000)#sym 点的形状,whis虚线的长度plt.boxplot(data,sym="o",whis=1.5)plt.show()</strong>

Copy after login

1

<strong>#sym 点的形状,whis虚线的长度</strong>

Copy after login

The above is the detailed content of Detailed explanation of examples of drawing graphics with python. For more information, please follow other related articles on 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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles