入门学习(一)matplotlib绘图可视化

入门学习(一)matplotlib绘图可视化

1.导入相关的包

import numpy as np
import matplotlib.pyplot as plt    #可视化数据库

2.生成数据,定义函数,描点画图

x = np.linspace(-np.pi, np.pi, 256, endpoint =True)  #从-π到π,256个点,
C,S = np.cos(x), np.sin(x)
plt.plot(x,C, color = 'red', linewidth = 2.5)        #plt.plot函数用于描点绘图
plt.plot(x,S, color = 'blue', linewidth = 2.5)
plt.show()                                           #打印出图像,不需要print

这里写图片描述

3.设置图像的属性

plt.figure(figsize = (15,20), dpi = 90)   #图像大小15*20,像素90
plt.subplot(111)
x = np.linspace(-5,5,250, endpoint=True)       #x的赋值,从-5到5,中间描点250个(点越多曲线越平滑)
C,S = np.cos(x), np.sin(x)

plt.plot(x,C,color = 'orange', linewidth = 2.5, linestyle = "-",label = 'cosine')
plt.plot(x,S,color = 'purple', linewidth = 1.0, linestyle = "--",label = 'sine')
plt.legend(loc='upper left', frameon = False)     #设置标签 在左上方
plt.xlim(-6.5,6.5)             #x轴的极限域
plt.xticks(np.linspace(-6,6,7, endpoint=True))   #x轴的刻度,分别是-6到6,中间有7的刻度
plt.ylim(-2,2)             #y轴的极限域
plt.yticks(np.linspace(-1.5,1.5,5,endpoint= True))
plt.show()

这里写图片描述

4.更改坐标轴的名称

plt.xticks([-6,-3,0,3,6],[r'$2001$',r'2002',r'2003',r'2004',r'$2005$']) #将横坐标5个刻度替换为年份

这里写图片描述

5.移动坐标轴

#删除plt.subplot(111),替换为以下:
ax = plt.subplot(111)
ax.spines['right'].set_color('none')  #隐藏右边的轴
ax.spines['top'].set_color('none')    #隐藏左边的轴
ax.xaxis.set_ticks_position('bottom')  #将下边的轴变成横轴
ax.spines['bottom'].set_position(('data',0))  #将横轴的中间设置为“0”
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

x = np.linspace(-5,5,250, endpoint=True)       #x的赋值,从-5到5,中间描点250个(点越多曲线越平滑)
C,S = np.cos(x), np.sin(x)

plt.plot(x,C,color = 'orange', linewidth = 2.5, linestyle = "-",label = 'cosine')
plt.plot(x,S,color = 'purple', linewidth = 1.0, linestyle = "--",label = 'sine')
plt.legend(loc='upper left', frameon = False)     #设置标签 在左上方
plt.xlim(-6.5,6.5)             #x轴的极限域
plt.xticks(np.linspace(-6,6,7, endpoint=True))   #x轴的刻度,分别是-6到6,中间有7的刻度
plt.ylim(-2,2)
plt.yticks(np.linspace(-1.5,1.5,5,endpoint= True))

plt.xticks([-6,-3,0,3,6],[r'$2001$',r'2002',r'2003',r'2004',r'$2005$'])
plt.show()

这里写图片描述

6.应用:预测波士顿房价

使用ggplot线性回归

# Import necessary packages
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')               #使用'ggplot'风格的图
from sklearn import datasets
from sklearn import linear_model
import numpy as np
# Load data下载数据
boston = datasets.load_boston()
yb = boston.target.reshape(-1, 1)    #boston.target获取数据的预测标签,转置成n行1列,-1表示行数不确定
Xb = boston['data'][:,5].reshape(-1, 1)  #boston['data']是获取列索引为“data”的列,[:,5]——":"表示提取数据所有的行,“5”表示前5列
# Plot data描点
plt.scatter(Xb,yb)                       #散点坐标
plt.ylabel('value of house /1000 ($)')    #给纵轴加标签
plt.xlabel('number of rooms')
# Create linear regression object线性回归
regr = linear_model.LinearRegression()       #定义函数
# Train the model using the training sets用训练集训练模型
regr.fit( Xb, yb)                            #调用函数
# Plot outputs
plt.scatter(Xb, yb,  color='black')
plt.plot(Xb, regr.predict(Xb), color='blue',linewidth=3)
plt.show()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jammy33/article/details/81673984
今日推荐