import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
# Python 中万物皆对象,Matplotlib 里这些元素也都是对象。下面代码打印出坐标系、坐标轴和刻度。
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # 增加一个子图
plt.show()
# 绘制x轴、y轴对象
xax = ax.xaxis
yax = ax.yaxis
# xax和yax指向的都是figure对象
print(xax.figure)
print(yax.figure)
r_hex = '#dc2624' # red, RGB = 220,38,36
dt_hex = '#2b4750' # dark teal, RGB = 43,71,80
tl_hex = '#45a0a2' # teal, RGB = 69,160,162
r1_hex = '#e87a59' # red, RGB = 232,122,89
tl1_hex = '#7dcaa9' # teal, RGB = 125,202,169
g_hex = '#649E7D' # green, RGB = 100,158,125
o_hex = '#dc8018' # orange, RGB = 220,128,24
tn_hex = '#C89F91' # tan, RGB = 200,159,145
g50_hex = '#6c6d6c' # grey-50, RGB = 108,109,108
bg_hex = '#4f6268' # blue grey, RGB = 79,98,104
g25_hex = '#c7cccf' # grey-25, RGB = 199,204,207
plt.figure()
plt.text(x=0.5,y=0.5,s='Figure',ha='center',va='center',size=20,alpha=0.5)
# 第一、二个参数是指横轴和纵轴坐标
# 第三个参数字符是指要显示的内容
# ha, va 是横向和纵向位置
# size 设置字体大小
# alpha 设置字体透明度 (0.5 是半透明)
from PIL import Image #导入图片处理模块
plt.figure()
plt.xticks([]) #将x、y的刻度置为null
plt.yticks([])
im = np.array(Image.open('111.png'))
plt.imshow(im)
plt.show()
plt.figure()
plt.plot([0,1],[0,1])
plt.show()
# subplot(rows, columns, i-th plots)
plt.figure()
plt.subplot(2,1,1)
plt.xticks([])
plt.yticks([])
plt.text(x=0.5,y=0.5,s='subplot(2,1,1)',ha='center',va='center',size=20,alpha=0.5)
plt.subplot(2,1,2)
plt.xticks([])
plt.yticks([])
plt.text(x=0.5,y=0.5,s='subplot(2,1,2)',ha='center',va='center',size=20,alpha=0.5)
plt.show()
# 声明完子图后,下面所有代码就只在这幅子图上生效,直到声明下一幅子图
# 循环绘制子图
fig,axes = plt.subplots(nrows=2,ncols=2)
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)
# 组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
for i,ax in enumerate(axes.flat):
ax.set(xticks=[],yticks=[])
s = 'subplot(2,2' + str(i) + ')'
ax.text(0.5,0.5,s,size=20,alpha=0.5,ha='center',va='center')
# 循环绘制子图
fig,axes = plt.subplots(nrows=2,ncols=2)
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)
# 组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
for i,ax in enumerate(axes.flat):
print(i)
print(ax)
## 不规则网格
import matplotlib.gridspec as gridspec
G = gridspec.GridSpec(3,3)
ax1 = plt.subplot(G[0:1,:3]) #类似于numpy的选取方式,选取矩阵数据
plt.xticks([]),plt.yticks([])
plt.text(x=0.5,y=0.5,s='Axes 1',ha='center',va='center',size=20,alpha=0.5)
ax2 = plt.subplot(G[1:2,0:2]) #类似于numpy的选取方式,选取矩阵数据
plt.xticks([]),plt.yticks([])
plt.text(x=0.5,y=0.5,s='Axes 2',ha='center',va='center',size=20,alpha=0.5)
ax3 = plt.subplot(G[1:3,2:3]) #类似于numpy的选取方式,选取矩阵数据
plt.xticks([]),plt.yticks([])
plt.text(x=0.5,y=0.5,s='Axes 3',ha='center',va='center',size=20,alpha=0.5)
ax4 = plt.subplot(G[2:3,0:1]) #类似于numpy的选取方式,选取矩阵数据
plt.xticks([]),plt.yticks([])
plt.text(x=0.5,y=0.5,s='Axes 4',ha='center',va='center',size=20,alpha=0.5)
ax5 = plt.subplot(G[2:3,1:2]) #类似于numpy的选取方式,选取矩阵数据
plt.xticks([]),plt.yticks([])
plt.text(x=0.5,y=0.5,s='Axes 5',ha='center',va='center',size=20,alpha=0.5)
plt.show()
# 大图套小图
plt.axes([0.1,0.1,0.8,0.8])
# plt.xticks([]),plt.yticks([])
plt.text(x=0.6,y=0.6,s='axes([0.1,0.1,0.8,0.8])',va='center',ha='center',size=20,alpha=0.5)
plt.axes([0.2,0.2,0.3,0.3])
plt.text(x=0.5,y=0.5,s='axes([0.2,0.2,0.3,0.3])',va='center',ha='center',size=10,alpha=0.5)
plt.show()
# 此时的axes对象的坐标和xticks刻度上的坐标并不一致
# plt.axes([l,b,w,h])
# 其中 [l, b, w, h] 可以定义坐标系
# l 代表坐标系左边到 Figure 左边的水平距离
# b 代表坐标系底边到 Figure 底边的垂直距离
# w 代表坐标系的宽度
# h 代表坐标系的高度
# 如果 l, b, w, h 都小于 1,那它们是标准化 (normalized) 后的距离。比如 Figure 底边长度为 10, 坐标系底边到它的垂直距离是 2,那么 b = 2/10 = 0.2。
# 第一种,同时生成图和坐标系:
fig,ax = plt.subplots()
ax.set(xticks=[],yticks=[])
s='xxx'
ax.text(x=0.5,y=0.5,s=s,ha='center',va='center',size=20,alpha=0.5)
plt.show()
# 第二种,先生成图,再添加坐标系
fig=plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set(xticks=[],yticks=[])
s='xxx'
ax.text(x=0.5,y=0.5,s=s,ha='center',va='center',size=20,alpha=0.5)
plt.show()
fig, ax = plt.subplots()
ax.set_xlabel('x label',size=20,color='red',rotation=30)
#所有的文本对象都可以调整文字和颜色以及角度
ax.set_ylabel('t label')
for label in ax.xaxis.get_ticklabels():
# x轴的label是文本对象,都可以调整相关文本的参数,y轴同样
label.set_color('blue')
label.set_rotation(45)
label.set_fontsize(20)
for line in ax.yaxis.get_ticklines():
# y轴的line对象进行调整,线条的对象同样有类似的属性
line.set_color('green')
line.set_markersize(500)
line.set_markeredgewidth(30)
字段含义如下:
SaleID - 销售样本ID
name - 汽车编码
regDate - 汽车注册时间
model - 车型编码
brand - 品牌
bodyType - 车身类型
fuelType - 燃油类型
gearbox - 变速箱
power - 汽车功率
kilometer - 汽车行驶公里
notRepairedDamage - 汽车有尚未修复的损坏
regionCode - 看车地区编码
seller - 销售方
offerType - 报价类型
creatDate - 广告发布时间
price - 汽车价格
v_0', 'v_1', 'v_2', 'v_3', 'v_4', 'v_5', 'v_6', 'v_7', 'v_8', 'v_9', 'v_10', 'v_11', 'v_12', 'v_13','v_14' 【匿名特征,包含v0-14在内15个匿名特征】
数字全都脱敏处理,都为label encoding形式,即数字形式
# 读取天池二手车交易价格预测的训练数据
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dftrain = pd.read_csv(r'.\\天池二手车交易交额预测\\used_car_train_20200313.csv',sep=' ')
dftrain.head()
# 对数据的基础数据了解下
dftrain.info()
dftrain.describe().round(2)
dftrain.corr()