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

How to draw a line chart using python

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 13:26:225318browse

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


##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")


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


The results are as follows:



Code explanation:

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


3. Draw sinx curve

The code is as follows:


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

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

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

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

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

#X-axis text

plt.xlabel("Time(s)")

#Y-axis text

plt.ylabel("Volt")

#Chart title

plt.title("PyPlot First Example")

#Range of Y-axis

plt.ylim(-1.2,1.2)

#Display diagram

plt.legend()

#Display diagram

plt.show()

#Save diagram

plt.savefig("sinx.jpg")


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") #保存图


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!

Statement:
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