2020.12.29
173行
import matplotlib.pyplot as plt #导入绘图库
import numpy as np
#Figure对象,子图的形状大小等
#figure(num,figsize,dpi,facecolor,edgecolor,frameon)
#num:图形编号或名称,取值为数组/字符串
#figsize:绘图对象的宽和高,单位为英寸
#dpi:绘图对象的分辨率,缺省值为80
#facecolor:背景颜色
#edgecolor:边框颜色。
#frameon:表示是否显示边框。
plt.figure(figsize=(10,1),facecolor="pink")
plt.plot()
plt.show()

#划分子图subplot
fig= plt.figure()
plt.subplot(221)
plt.subplot(222)
plt.subplot(223)
plt.show()

#设置中文字体
# 字体 中文黑体
plt.rcParams["font.sans-serif"]="SimHei"
plt.rcParams["axes.unicode_minus"]=False #正常显示数组
#plt.rcdefaults() #恢复标准默认配置
#添加标题
#全局标题 suptitle() 子标题 title()
#散点图
#scatter(x,y,scale,color,marker,label)
#scale 数据点的大小 默认值 36
#color 数据点颜色
#marker 数据点的样式 圆点
#label 图例文字
n=1024
x1=np.random.normal(0,1,n) #标准正态分布
y1=np.random.normal(0,1,n)
plt.scatter(x1,y1,4,color="blue",marker='*',label="正态分布") #绘制散点图
plt.title("标准正态分布",fontsize=20)
#text(x,y,s,fontsize,color)
#在图表中添加文字. # s 为添加的内容
plt.text(2.5,2.5,"均 值:0\n标准差:1")
#设置坐标轴范围
plt.xlim(-5,5) #xlim(xmin,xmax)
plt.ylim(-5,5) #xlim(ymin,ymax)
#设置坐标轴标签
plt.xlabel('横坐标x',fontsize=14) #xlabel(x,y,s,fontsize,color)
plt.ylabel('纵坐标y',fontsize=14) #ylabel(x,y,s,fontsize,color)
x2=np.random.uniform(-4,4,(1,n)) #在图像中添加新的点
y2=np.random.uniform(-4,4,(1,n))
plt.scatter(x2,y2,4,color="red",label="均匀分布")
plt.legend()

#折线图 plt.plot(x,y,color,marker,label,linewidth,markersize)
#linewidth 折线的宽度
n=24
y1=np.random.randint(27,37,n)
y2=np.random.randint(40,60,n)
plt.plot(y1,label='温度')
plt.plot(y2,label='湿度')
plt.legend()
plt.xlabel('小时',fontsize=12)
plt.ylabel('测量值',fontsize=12)
plt.xlim(0,23)
plt.ylim(20,70)
plt.title('24小时温度湿度统计',fontsize=16)

#柱状图 plt.bar(left,height,width,facecolor,edgecolor,label)
# left 柱子左边缘的位置
# height 柱子高度
# width 柱子的宽度
# facecolor 柱子的填充色
# edgecolor 柱子边框的
plt.bar(range(len(y1)),y1,width=0.8,facecolor='green',label='统计量1')

#加载数据集
import tensorflow as tf
boston_housing= tf.keras.datasets.boston_housing
(train_x,train_y),(test_x,test_y)= boston_housing.load_data(test_split=0)
#test_split可改变测试集和训练集的比例
print("Training set:",len(train_x)) #训练集中的数据量
print("Testing set:",len(test_x)) #测试集中的数据量
#访问数据集中的数据
print(type(train_x))
print(type(test_x))
print("维度 of train_x:",train_x.ndim) #训练集的维度
print("shape of train_x",train_x.shape) #训练集的形状
print("维度 of train_y:",train_y.ndim)
print("shape of train_y",train_y.shape)
plt.figure(figsize=(5,5))
plt.scatter(train_x[:, 5],train_y,10)
# 意思是第几个属性值
plt.xlabel("RM")
plt.ylabel("Price($1000's)")
plt.title("5. RM-Price")
plt.show()
titles = ["CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS",
"RAD","TAX","PTRATIO","B-1000","LSTAT","MEDV"]
plt.figure(figsize=(12,12))
#用循环来将各个属性与房价之间的关系显示出来
for i in range(13):
plt.subplot(4,4,(i+1))
plt.scatter(train_x[:,i],train_y,5) #i为第i个属性
plt.xlabel(titles[i])
plt.suptitle("各个属性与房价的关系",x=0.5,y=1.02,fontsize=20)
plt.show()

#tf.keras.utils.get_file(fname,origin,cache_dir)
#fname:下载后的文件名
#origin:文件的URL地址
#cache_dir:下载后文件的储存位置
TRAIN_URL="http://download.tensorflow.org/data/iris_training.csv"
train_path=tf.keras.utils.get_file("iris_training.csv",TRAIN_URL)
##train_path=tf.keras.utils.get_file(TRAIN_URL.split('/')[-1],TRAIN_URL)
#以后用上面语句进行下载文件,只需要修改网址URL
#访问csv文件
#Pandas库,用于数据统计和分析,可以高效,方便地操作大型数据集
import pandas as pd #导入pandas库
# pd.read_csv(路径,header,names) 读取csv数据集文件
# header=0;将第一行数据作为列标题
# names :自定义列标题,代替header参数指定的列标题
COLUMN_NAMES=['SepalLength','SepalWidth','PetalLength','PetalWidth','Species']
df_iris=pd.read_csv("D:\python\datas\iris_training.csv")
pd.read_csv("D:\python\datas\iris_training.csv",names=COLUMN_NAMES,header=0)

print(type(df_iris)) #二维数据
NAMES=['120','4','山莺尾','变色莺尾','维吉尼亚莺尾']
pd.read_csv("D:\python\datas\iris_training.csv",names=NAMES,header=0)

#head(n) :默认读取二维数据表中的前5行数据
#tail(n) : 读取后n行数据,默认后5行
pd.read_csv("D:\python\datas\iris_training.csv",names=COLUMN_NAMES,header=0)
df_iris.head(10)
df_iris.tail(10)
df_iris[10:15] #使用索引和切片
df_iris.describe() #显示二维数据的统计信息
#总数,平均值,标准差,

print(df_iris.ndim)
print(df_iris.shape)
print(df_iris.size) #数据总数
#将图标类型转化为Numpy数组,再用于制图,以下三种方法等价于
iris=np.array(df_iris)
#iris=df_iris.values
#iris=df_iris.as_matrix()
print(iris[0:6])
iris[:,2]
#绘制散点图
#plt.scatter(x,y,c,cmap) #将参数c指定一个列表或元组,图标的颜色可以随元素不同而改变
#
plt.scatter(iris[:,2],iris[:,3],9,c=iris[:,4],cmap='brg')
plt.xlabel("SepalWidth")
plt.ylabel("PetalLength")
plt.title("莺尾花的种类与萼片宽度和长度的关系\n(蓝色->山莺尾|红色->变色莺尾|绿色->维吉尼亚莺尾)")
plt.show

#运用循环来输出
plt.figure(figsize=(15, 3)) #用于设置绘图尺寸大小
for p in range(4):
plt.subplot(1, 4,p+1)
if(p==0):
plt.text(0.3,0.5,"SepalLength",fontsize=15)
else:
plt.scatter(iris[:,p],iris[:,0],9,c=iris[:,4],cmap='brg')
plt.title(COLUMN_NAMES[p])
plt.ylabel(COLUMN_NAMES[0])
plt.tight_layout(rect=[0,0,1,0.9]) #调整子图间距
plt.show()

plt.figure(figsize=(15,15)) #创建画布
plt.suptitle("莺尾花的种类与萼片宽度和长度的关系\n(蓝色->山莺尾|红色->变色莺尾|绿色->维吉尼亚莺尾)")
for i in range(4):
for j in range(4):
plt.subplot(4,4,4*i+(j+1)) #首先要创建子图
if(i==j):
plt.text(0.3,0.4,COLUMN_NAMES[i],fontsize=15)
else:
plt.scatter(iris[:,j],iris[:,i],11,c=iris[:,4],cmap='brg')
if(i==0):
plt.title(COLUMN_NAMES[j]) #横坐标标签
if(j==0):
plt.ylabel(COLUMN_NAMES[i]) #纵坐标标签
plt.tight_layout(rect=[0,0,1,0.93])

博客记录了2020年12月29日的相关内容,共173行,但未明确具体信息。


被折叠的 条评论
为什么被折叠?



