如何用python画出折线图
用pylab模块的plot函数
pylab.plot(x,y)其中x y都是数组
就能画出以x,y中元素为坐标的折线图
python3 折线图上如何显示特殊数据
# encoding=utf-8
import matplotlib.pyplot as plt
# 月份
x1 = ['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06', '2017-07', '2017-08',
'2017-09', '2017-10', '2017-11', '2017-12']
# 体重
y1 = [86, 85, 84, 80, 75, 70, 70, 74, 78, 70, 74, 80]
# 设置画布大小
plt.figure(figsize=(16, 4))
# 标题
plt.title("my weight")
# 数据
plt.plot(x1, y1, label='weight changes', linewidth=3, color='r', marker='o',
markerfacecolor='blue', markersize=20)
# 横坐标描述
plt.xlabel('month')
# 纵坐标描述
plt.ylabel('weight')
# 设置数字标签
for a, b in zip(x1, y1):
plt.text(a, b, b, ha='center', va='bottom', fontsize=20)
plt.legend()
plt.show()
python怎么画折线图
一、环境准备
linux ubuntu 下需安装下面三个包:
Numpy, Scipy,Matplotlib
分别输入下面的代码进行安装:
[plain] view plain copy
pip install numpy
pip install scipy
sudo apt-get install python-matplotlib
测试是否安装成功
[html] view plain copy
python
>>> import pylab
如果没有报错则安装成功
二、开始画图
1. 画最简单的直线图
代码如下:
[python] view plain copy
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")
结果如下:
代码解释:
[python] view plain copy
#x轴,y轴
x=[0,1]
y=[0,1]
#创建绘图对象
plt.figure()
#在当前绘图对象进行绘图(两个参数是x,y轴的数据)
plt.plot(x,y)
#保存图象
plt.savefig("easyplot.jpg")
2. 给图加上标签与标题
上面的图没有相应的X,Y轴标签说明与标题
在上述代码基础上,可以加上这些内容
代码如下:
[python] view plain copy
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")
结果如下:
代码解释:
[python] view plain copy
plt.xlabel("time(s)") #X轴标签
plt.ylabel("value(m)") #Y轴标签
plt.title("A simple plot") #标题
3. 画sinx曲线
代码如下:
[python] view plain copy
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
#设置x,y轴的数值(y=sinx)
x = np.linspace(0, 10, 1000)
y = np.sin(x)
#创建绘图对象,figsize参数可以指定绘图对象的宽度和高度,单位为英寸,一英寸=80px
plt.figure(figsize=(8,4))
#在当前绘图对象中画图(x轴,y轴,给所绘制的曲线的名字,画线颜色,画线宽度)
plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)
#X轴的文字
plt.xlabel("Time(s)")
#Y轴的文字
plt.ylabel("Volt")
#图表的标题
plt.title("PyPlot First Example")
#Y轴的范围
plt.ylim(-1.2,1.2)
#显示图示
plt.legend()
#显示图
plt.show()
#保存图
plt.savefig("sinx.jpg")
结果如下:
4. 画折线图
代码如下:
[python] view plain copy
# -*- 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") #保存图
结果如下:
python 画折线图加一个legend
12345import matplotlib.pyplot as plt plt.plot([1,2,4,1], label='line1')plt.plot([3,1,2,3], label='line2')plt.legend()
可以通过plot函数的label参数来设置,
然后需要调用一下legend()函数。
【本文标题和网址】python显示股票折线图-python怎么画折线图:http://www.jjta.cn/pzzx/273465.html
声明本站分享的文章旨在促进信息交流,不以盈利为目的,本文观点与本站立场无关,不承担任何责任。如有伤害到您的利益,请联系站长删除!