用python画折线图
⑴ python怎么画折线图
一、环境准备
linux ubuntu 下需安装下面三个包:
Numpy, Scipy,Matplotlib
分别输入下面的代码进行安装:
[plain]view plain
pipinstallnumpy
pipinstallscipy
sudoapt-getinstallpython-matplotlib
python
>>>importpylab
importnumpyasnp
importmatplotlib.pyplotasplt
x=[0,1]
y=[0,1]
plt.figure()
plt.plot(x,y)
plt.savefig("easyplot.jpg")
测试是否安装成功
[html]view plain
如果没有报错则安装成功
二、开始画图
1. 画最简单的直线图
代码如下:
[python]view plain
结果如下:

⑵ 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
转自 跳转链接
一、用默认设置绘制折线图
import matplotlib.pyplot as plt
x_values=list(range(11))
#x轴的数字是0到10这11个整数
y_values=[x**2 for x in x_values]
#y轴的数字是x轴数字的平方
plt.plot(x_values,y_values,c='green')
#用plot函数绘制折线图,线条颜色设置为绿色
plt.title('Squares',fontsize=24)
#设置图表标题和标题字号
plt.tick_params(axis='both',which='major',labelsize=14)
#设置刻度的字号
plt.xlabel('Numbers',fontsize=14)
#设置x轴标签及其字号
plt.ylabel('Squares',fontsize=14)
#设置y轴标签及其字号
plt.show()
#显示图表
制作出图表
我们希望x轴的刻度是0,1,2,3,4……,y轴的刻度是0,10,20,30……,并且希望两个坐标轴的范围都能再大一点,所以我们需要手动设置。
二、手动设置坐标轴刻度间隔以及刻度范围
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
x_values=list(range(11))
y_values=[x**2 for x in x_values]
plt.plot(x_values,y_values,c='green')
plt.title('Squares',fontsize=24)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.xlabel('Numbers',fontsize=14)
plt.ylabel('Squares',fontsize=14)
x_major_locator=MultipleLocator(1)
#把x轴的刻度间隔设置为1,并存在变量里
y_major_locator=MultipleLocator(10)
#把y轴的刻度间隔设置为10,并存在变量里
ax=plt.gca()
#ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)
#把x轴的主刻度设置为1的倍数
ax.yaxis.set_major_locator(y_major_locator)
#把y轴的主刻度设置为10的倍数
plt.xlim(-0.5,11)
#把x轴的刻度范围设置为-0.5到11,因为0.5不满一个刻度间隔,所以数字不会显示出来,但是能看到一点空白
plt.ylim(-5,110)
#把y轴的刻度范围设置为-5到110,同理,-5不会标出来,但是能看到一点空白
plt.show()
绘制结果
⑶ 如何用python绘制各种图形
1.环境
系统:windows10
python版本:python3.6.1
使用的库:matplotlib,numpy
2.numpy库产生随机数几种方法
import numpy as np
numpy.random
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)查询结果为标准正态分布
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.44852833, -0.10858996, -1.65034606, -0.39793635]])
randint(low,high,size)
生成low到high之间(半开区间 [low, high)),size个数据
In [6]: x=np.random.randint(1,8,4)
In [7]: x
Out[7]: array([4, 4, 2, 7])
random_integers(low,high,size)
生成low到high之间(闭区间 [low, high)),size个数据
In [10]: x=np.random.random_integers(2,10,5)
In [11]: x
Out[11]: array([7, 4, 5, 4, 2])
3.散点图
x x轴
y y轴
s 圆点面积
c 颜色
marker 圆点形状
alpha 圆点透明度#其他图也类似这种配置
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()

8.箱型图
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()
#sym 点的形状,whis虚线的长度
⑷ python画折线图
#encoding=utf-8
importmatplotlib.pyplotasplt
frompylabimport*#支持中文
mpl.rcParams['font.sans-serif']=['SimHei']
names=['5','10','15','20','25']
x=range(len(names))
y=[0.855,0.84,0.835,0.815,0.81]
y1=[0.86,0.85,0.853,0.849,0.83]
#plt.plot(x,y,'ro-')
#plt.plot(x,y1,'bo-')
#pl.xlim(-1,11)#限定横轴的范围
#pl.ylim(-1,110)#限定纵轴的范围
plt.plot(x,y,marker='o',mec='r',mfc='w',label=u'y=x^2曲线图')
plt.plot(x,y1,marker='*',ms=10,label=u'y=x^3曲线图')
plt.legend()#让图例生效
plt.xticks(x,names,rotation=45)
plt.margins(0)
plt.subplots_adjust(bottom=0.15)
plt.xlabel(u"time(s)邻居")#X轴标签
plt.ylabel("RMSE")#Y轴标签
plt.title("Asimpleplot")#标题
plt.show()

