Python1.2(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24477135/article/details/79628949

利用matplotlib.pyplot中的subplots画图,将数据分析结果可视化。具体意思可以根据代码理解。

import os
import pandas as pd
import requests
import matplotlib.pyplot as plt
plt.style.use('ggplot')  #将风格设置为近似R中的ggplot库  #下一行设置插图,让他们能够在记事本中可见
%matplotlib inline  
import numpy as np


PATH = r'C:/Users/Administrator/Desktop/iris/'
#PATH = os.getcwd()

r = requests.get('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data')
#print(r.text)
with open(PATH + 'iris.data' , 'w') as f:
    f.write(r.text)
    
os.chdir(PATH)

df = pd.read_csv(PATH + 'iris.data' , names = ['sepal length' , 'sepal width' , 'petal length' , 'petal width' , 'class'])
fig , ax = plt.subplots(figsize=(6,4))    #创建了宽度为6英寸,高度为4英寸的一个插图
ax.hist(df['petal width'] , color = 'black')         #调用hist传入数据并将直方图柱子颜色设置为黑色
ax.set_ylabel('Count' , fontsize = 12)                #设置y轴标签
ax.set_xlabel('Width' , fontsize = 12)                #设置x轴标签
plt.title("Iris Petal Width" , fontsize = 14 , y = 1.01)          #设置标题,利用y轴参数调整了标题在y轴方向上对于图片顶部的位置

fig , ax = plt.subplots(2, 2 , figsize = (6 , 4))     #生成2行列的四个子图,然后利用ax去绘制

ax[0][0].hist(df['petal width'] , color = 'black')
ax[0][0].set_ylabel('Count' , fontsize = 12)
ax[0][0].set_xlabel('Width' , fontsize = 12)
ax[0][0].set_title('Iris Petal Width' , fontsize = 14 , y = 1.01)

ax[0][1].hist(df['petal length'] , color = 'black')
ax[0][1].set_ylabel('Count' , fontsize = 12)
ax[0][1].set_xlabel('Length' , fontsize = 12)
ax[0][1].set_title('Iris Petal Length' , fontsize = 14 , y = 1.01)

ax[1][0].hist(df['sepal width'] , color = 'black')
ax[1][0].set_ylabel('Count' , fontsize = 12)
ax[1][0].set_xlabel('Width' , fontsize = 12)
ax[1][0].set_title('Iris Sepal Width' , fontsize = 14 , y = 1.01)

ax[1][1].hist(df['sepal length'] , color = 'black')
ax[1][1].set_ylabel('Count' , fontsize = 12)
ax[1][1].set_xlabel('Length' , fontsize = 12)
ax[1][1].set_title('Iris Sepal Length' , fontsize = 14 , y = 1.01)

plt.tight_layout()                #自动调整子插图,一面排版过于拥挤


fig , ax  = plt.subplots(figsize = (6 , 6))
ax.scatter(df['petal width'] , df['petal length'] , color = 'green')           #绘制散点图并导入数据,点的颜色设置为green
ax.set_xlabel('Petal Width')
ax.set_ylabel('Petal Length')
ax.set_title('Petal Scatterplot')

fig , ax = plt.subplots(figsize = (6 , 6))
ax.plot(df['petal length'] , color = 'blue')           #线图
ax.set_xlabel('Specimen Number')
ax.set_ylabel('Petal Length')
ax.set_title('Petal Length Plot')

fig , ax = plt.subplots(figsize=(6 , 6))
bar_width = .8
labels = [x for x in df.columns if 'length' in x or 'width' in x]    #获取标签,含有length或者width的列
ver_y = [df[df['class'] == 'Iris-versicolor'][x].mean() for x in labels]
vir_y = [df[df['class'] == 'Iris-virginica' ][x].mean() for x in labels]
set_y = [df[df['class'] == 'Iris-setosa'][x].mean() for x in labels]
x = np.arange(len(labels))        #创建等差数组,即每个标签所对应的条形图的起始位置
ax.bar(x , vir_y , bar_width , bottom = set_y , color = 'darkgrey')  #bar函数中有四个参数是用来界定柱体位置及形状的,分别为x、height、width、bottom,它们分别代表柱体位置、柱体高度、柱体宽度、柱体底部位置。
ax.bar(x , set_y , bar_width , bottom = ver_y , color = 'white')  #https://baijiahao.baidu.com/s?id=1591622075115413974&wfr=spider&for=pc
ax.bar(x , ver_y , bar_width , color = 'black')   #三种类型一次叠加
ax.set_xticks(x + (bar_width)/2)            #设置x的下一个条形的偏移
ax.set_xticklabels(labels , rotation = -70 , fontsize = 12)        #设置横坐标的区分,即有几个柱,labels中有四个类别就有四个柱子
ax.set_title('Mean Feature Measurement By Class' , y = 1.01)
ax.legend(['Virginica' , 'Setosa' , 'Versicolor'])     #设置图例

结果如下:






猜你喜欢

转载自blog.csdn.net/qq_24477135/article/details/79628949
1.2