Home > Backend Development > PHP Tutorial > How to draw a line chart using python

How to draw a line chart using python

php中世界最好的语言
Release: 2023-03-18 06:36:01
Original
5366 people have browsed it

Today I will teach you how to draw some linear patterns using python. Friends in need can refer to it.

Draw the simplest straight line graph

The code is as follows:

import numpy as np
import matplotlib.pyplot as plt
  
x=[0,1]
y=[0,1]
plt.figure()
plt.plot(x,y)
plt.savefig("easyplot.jpg")
Copy after login


##The result is as follows:


Code explanation:

#x轴,y轴
x=[0,1]
y=[0,1]
#创建绘图对象
plt.figure()
#在当前绘图对象进行绘图(两个参数是x,y轴的数据)
plt.plot(x,y)
#保存图象
plt.savefig("easyplot.jpg")
Copy after login


2. Add a label to the picture and the title

does not correspond to the picture above The X, Y axis label description and title

Based on the above code, you can add these contents

The code is as follows:

   
import numpy as np
import matplotlib.pyplot as plt
  
x=[0,1]
y=[0,1]
  
plt.figure()
plt.plot(x,y)
plt.xlabel("time(s)")
plt.ylabel("value(m)")
plt.title("A simple plot")
Copy after login


The results are as follows:



Code explanation:

   
plt.xlabel("time(s)") #X轴标签
plt.ylabel("value(m)") #Y轴标签
plt.title("A simple plot") #标题
Copy after login


3. Draw sinx curve

The code is as follows:


# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
Copy after login

#Set the values ​​of x and y axes (y=sinx)

x = np.linspace(0, 10, 1000)
y = np.sin(x)
Copy after login

#Create a drawing object, the figsize parameter can specify the width and height of the drawing object, the unit is inches, one inch = 80px

plt.figure(figsize=(8,4))
Copy after login

#Draw in the current drawing object (x-axis, y-axis, for all Name of the drawn curve, line color, line width)

plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)
Copy after login

#X-axis text

plt.xlabel("Time(s)")
Copy after login

#Y-axis text

plt.ylabel("Volt")
Copy after login

#Chart title

plt.title("PyPlot First Example")
Copy after login

#Range of Y-axis

plt.ylim(-1.2,1.2)
Copy after login

#Display diagram

plt.legend()
Copy after login

#Display diagram

plt.show()
Copy after login

#Save diagram

plt.savefig("sinx.jpg")
Copy after login


The results are as follows:


4. Draw a line chart

The code is as follows:

 
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
#X轴,Y轴数据
x = [0,1,2,3,4,5,6]
y = [0.3,0.4,2,5,3,4.5,4]
plt.figure(figsize=(8,4)) #创建绘图对象
plt.plot(x,y,"b--",linewidth=1)  #在当前绘图对象绘图(X轴,Y轴,蓝色虚线,线宽度)
plt.xlabel("Time(s)") #X轴标签
plt.ylabel("Volt") #Y轴标签
plt.title("Line plot") #图标题
plt.show() #显示图
plt.savefig("line.jpg") #保存图
Copy after login


I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

Detailed code examples of how to implement stack data structure and bracket matching algorithm in php

The most popular in php Simple string matching algorithm, php matching algorithm_PHP tutorial

The simplest string matching algorithm tutorial in php

The above is the detailed content of How to draw a line chart using python. For more information, please follow other related articles on 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