发布时间:2023-02-09 08:00
1.画散点图(plt.scatter函数)
(1)列表数据画散点图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
height = [168,150,200,190,185,100,125,180]
weight = [60,65,73,70,65,58,66,67]
plt.figure(figsize=(8, 4)) #画板尺寸
plt.scatter(height, weight,color='red',marker='+') #画散点图必须有两列数据
plt.title('beauty') #图形标题
plt.xlabel('weight') #X轴标签
plt.ylabel('height') #Y轴标签
plt.show()
(2)数组数据画散点图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
height =np.array([168,150,200,190,185,100,125,180])
weight =np.array([60,65,73,70,65,58,66,67])
plt.figure(figsize=(8, 4)) #画板尺寸
plt.scatter(height, weight) #画散点图必须有两列数据
plt.title('beauty') #图形标题
plt.xlabel('weight') #X轴标签
plt.ylabel('height') #Y轴标签
plt.show()
(3)Series画散点图
forest_fires = pd.read_csv('forest_fires.csv')
plt.figure(figsize=(8,4))
plt.scatter(forest_fires["wind"],forest_fires["area"],c="red") #须有两列数据
plt.grid(True) #是否显示网格
(4)DataFrame画散点图
np.random.seed(20)
data=pd.DataFrame(np.random.randn(1000,4),columns=list('ABCD'))
data=data.cumsum()
print(data.head())
plt.scatter(data['A'],data['B'],c='r')
plt.scatter(data['B'],data['C'],c='b')
plt.show()
(5)三维散点图绘制
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = np.random.randint(0, 255, size=[40, 40, 40])
x, y, z = data[0], data[1], data[2]
fig1 = plt.figure(1)
ax = Axes3D(fig1)
# 将数据点分成三部分画,在颜色上有区分度
ax.scatter(x[:10], y[:10], z[:10], c='b') # 绘制数据点
ax.scatter(x[10:20], y[10:20], z[10:20], c='y')
ax.scatter(x[30:40], y[30:40], z[30:40], c='r')
ax.set_zlabel('Z') # 坐标轴
ax.set_ylabel('Y')
ax.set_xlabel('X')
plt.show()
2.画连线图(plt.plot函数)
(1)列表数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
age = [5, 10, 15, 20, 25, 30]
high=[60,100,150,170,171,171]
plt.figure(figsize=(8,4))
plt.plot(age)#连线图可以只有一列数据,默认该列数据为纵坐标,横坐标为序号(从0开始)。
plt.plot(age, high)
plt.grid(True)
plt.title('Age vs High') #图形名称
plt.xlabel('age') #X轴标签
plt.ylabel('high') #Y轴标签
plt.show()
(2)DataFrame数据
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
np.random.seed(20)
data=pd.DataFrame(np.random.randn(1000,4),columns=list('MNOP'))
data=data.cumsum()
data.plot()
plt.show()
data.plot(x='P',y='N',kind='line')
plt.show()
3.频率直方图
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
s = np.random.normal(3,2,300)
plt.figure(figsize=(10,5))
plt.hist(s, bins=50, normed=True) #频率直方图,bins表示样本值分成多少个区间
#plt.hist(s, bins=50) #纵坐标是每个间隔中包含的值的数量,不是频率.
plt.show()