绘图
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
tf.__version__
tf.test.is_gpu_available()
绘制简单的框架
plt.figure(figsize=(3,2),num=111,facecolor='green')
plt.plot()
plt.show()
结果:
plt.rcParams["font.family"] = "SimHei"
fig = plt.figure(facecolor='lightgrey')
plt.subplot(221)
plt.title('title1')
plt.subplot(222)
plt.title('title2',loc='left',color='b')
plt.subplot(223)
myfontdic = {'fontsize':22,'color':'g','rotation':30}
plt.title('title3',fontdict=myfontdic)
plt.subplot(224)
plt.title('title4',color='white',backgroundcolor='black')
plt.suptitle('total_title',fontsize=20,color='red',backgroundcolor='yellow')
plt.tight_layout(rect=[0,0,1,0.9])
plt.show()
结果:
散点图
import numpy as np
plt.rcParams["font.family"] = "SimHei"
plt.rcParams['axes.unicode_minus'] = False
n = 222
x1 = np.random.normal(0,1,n)
y1 = np.random.normal(0,1,n)
x2=np.random.uniform(-4,4,(1,n))
y2=np.random.uniform(-4,4,(1,n))
plt.scatter(x1,y1,color='b',marker='*',label="正态分布")
plt.scatter(x2,y2,color='y',marker='o',label="均匀分布")
plt.title("标准正态分布",fontsize=20)
plt.xlim(-4,4)
plt.ylim(-4,4)
plt.xlabel('横坐标x',fontsize=14)
plt.ylabel('纵坐标y',fontsize=14)
plt.legend(loc=1)
plt.show()
折线图
plt.rcParams["font.family"] = "SimHei"
plt.rcParams['axes.unicode_minus'] = False
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.show()
条形图
plt.rcParams["font.family"] = "SimHei"
plt.rcParams['axes.unicode_minus'] = False
y1=[32,25,16,30,24,45,40,33,28,17,24,20]
y2=[-23,-35,-26,-35,-45,-43,-35,-32,-23,-17,-22,-28]
plt.bar(range(len(y1)),y1,width=0.8,facecolor='green',edgecolor='b',label='666')
plt.bar(range(len(y2)),y2,label='888')
plt.legend()
plt.show()
绘制波士顿房价二维图
加载数据
boston_housing = tf.keras.datasets.boston_housing
(tx,ty),(x,y) = boston_housing.load_data()
print(tx.ndim)
print(tx.shape)
绘制room—price散点图
plt.rcParams["font.family"] = "SimHei"
plt.rcParams['axes.unicode_minus'] = False
plt.scatter(tx[:,5],ty)
plt.xlabel('RM')
plt.ylabel('price')
plt.title('5. RM-P')
plt.show()
鸢尾花
加载数据
train_url = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(train_url.split('/')[-1],train_url)
import pandas as pd
print(train_path)
用pd读数据
df_iris=pd.read_csv(train_path)
type(df_iris)
df_iris.head()
df_iris.head(1)
df_iris.head(3)
df_iris.tail(2)
df_iris[10:16]
df_iris.ndim
column_names= ['sl','sw','pl','pw','s']
df_iris=pd.read_csv(train_path,names=column_names,header=0)
df_iris.head(3)
df_iris.describe()
iris = np.array(df_iris)
iris[0:6]
x =np.arange(10)
y=np.arange(10)
doc_color=[0,1,2,0,1,2,2,1,1,0]
plt.scatter(x,y,c=doc_color,cmap='brg')
plt.show()
绘制鸢尾花二维特征
plt.scatter(iris[:,2],iris[:,3],c=iris[:,4],cmap='brg')
plt.show()