目录
散点图
反应相关性
例1:num2的产生是与num1相关的,以num1为X、num2为Y绘制散点图可观察相关性
num1 = np.random.rand(100)
num2 = np.random.rand(100)*0.3+num1
plt.scatter(num1, num2)
plt.show()
条形图(柱状图)
能够使人们一眼看出各个数据的大小。易于比较数据之间的差别。
例1:绘制条形图
y = [20, 10, 30, 25, 15]
index = np.arange(5)
pl = plt.bar(x=index, height=y, color='r', width=0.2)
plt.show()
例2:绘制叠加条形图
增加参数bottom使图形上移完成叠加
y = [20, 10, 30, 25, 15]
z = [14, 13, 10, 29, 35]
index = np.arange(5)
pl = plt.bar(x=index, height=y, color='r', width=0.2)
pl = plt.bar(x=index, bottom=y, height=z, color='g', width=0.2)
plt.show()
例3:绘制对比条形图
将x坐标偏移形状宽度
y = [20, 10, 30, 25, 15]
z = [14, 13, 10, 29, 35]
index = np.arange(5)
pl = plt.bar(x=index, height=y, color='r', width=0.2)
pl = plt.bar(x=index+0.2, height=z, color='g', width=0.2)
plt.show()
例4:绘制水平条形图
注意参数变化
y = [20, 10, 30, 25, 15]
index = np.arange(5)
pl = plt.barh(y=index, width=y, color='r')
plt.show()
直方图(质量分布图)
反应质量分布频度(用于检验是否满足正态分布)
注意与条形图区别(直方图通常为连续变量)
例1:绘制直方图
bins:条数(bins越大曲线越平滑),density:是否显示为比例(True:按比例为坐标,False:按个数为坐标)
mu = 100 # mean of distribution
sigma = 20 # standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.hist(x, bins=100, color='red', density=False)
plt.show()
例2:不同参数直方图
注意纵坐标轴变化
mu = 100 # mean of distribution
sigma = 20 # standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.hist(x, bins=50, color='b', density=True)
plt.show()
例3:二维直方图
用于检验两组变量的联合分布密度
x = np.random.randn(1000)+1
y = np.random.randn(1000)+5
plt.hist2d(x,y,bins=50)
plt.show()
饼状图
反应各部分数据占总量的比值
例1:绘制饼状图
autopct:显示占比数值,explode:突出分离某部分,shadow:图像显示阴影
labels = 'A', 'B', 'C', 'D'
fracs = [15, 30, 45, 5]
plt.axes(aspect=1)
plt.pie(x = fracs, labels=labels, autopct='%.0f%%', explode=[0, 0.1, 0, 0], shadow=True)
plt.show()
盒须图(盒式图、盒状图或箱线图)
是一种用作显示一组数据分散情况资料的统计图。
它能显示出一组数据的最大值、最小值、中位数、及上下四分位数。
例1:绘制一个盒式图
data = np.random.normal(size=1000, loc=0, scale=1)
# sym:调整异常值样式,whis:上下边缘长度比例
plt.boxplot(data, sym='o', whis=1.5)
plt.show()
例2:多盒图像
data = np.random.normal(size=(1000,4), loc=5, scale=1)
labels = ['A', 'B', 'C', 'D']
# sym:调整异常值样式,whis:上下边缘长度
plt.boxplot(x=data, labels=labels, sym='>')
plt.show()
Style
颜色
matplotlib提供了多种设置颜色的方法
最常用的是指定颜色/RGB/十六进制代码几个方法
an RGB or RGBA (red, green, blue, alpha) tuple of float values in [0, 1] (e.g., (0.1, 0.2,
0.5) or (0.1, 0.2, 0.5, 0.3));
a hex RGB or RGBA string (e.g., ‘#0f0f0f’ or ‘#0f0f0f80’; case-insensitive);
a string representation of a float value in [0, 1] inclusive for gray level (e.g., ‘0.5’);
one of {‘b’, ‘g’, ‘r’, ‘c’, ‘m’, ‘y’, ‘k’, ‘w’};
a X11/CSS4 color name (case-insensitive);
a name from the xkcd color survey, prefixed with ‘xkcd:’ (e.g., ‘xkcd:sky blue’; case
insensitive);
220 Chapter 2. Tutorials
Matplotlib, Release 3.1.1
one of the Tableau Colors from the ’T10’ categorical palette (the default color
cycle): {‘tab:blue’, ‘tab:orange’, ‘tab:green’, ‘tab:red’, ‘tab:purple’, ‘tab:brown’,
‘tab:pink’, ‘tab:gray’, ‘tab:olive’, ‘tab:cyan’} (case-insensitive);
a ”CN” color spec, i.e. ‘C’ followed by a number, which is an index into the default property
cycle (matplotlib.rcParams[‘axes.prop_cycle’]); the indexing is intended to occur at
rendering time, and defaults to black if the cycle does not include color.