python, 基本统计图的绘制

1. 绘制条形图

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
iris_data = load_iris()
sample_1 = iris_data.data[0,:] # 取出第1行的所有数据
print(sample_1)
# 绘制条开图
p1 = plt.bar(range(1, len(sample_1) + 1),
             height = sample_1,
             tick_label = iris_data.feature_names,
             width = 0.3)
plt.ylabel('cm')
plt.title('plt of_first data')
plt.show()

  输出图形如下:

2. 饼图

import matplotlib.pyplot as plt
labels = 'Sunny', 'Windy', 'Frogy', 'Snowy' # 定义4种天气
sizes = [15, 30, 45, 10] # 定义4种天气所占的比例(%)
explode = (0, 0.1, 0, 0) # 饼图弹出第2个天气
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode = explode, labels = labels,
        autopct = '%1.1f%%', shadow = True, startangle = 90)
ax1.axis('equal')
plt.show()

3. 折线图

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

4. 直方图
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
iris_data = load_iris()
feature_2 = iris_data.data[:,1]
plt.hist(feature_2, bins = 10)
plt.show()

5. 散点图
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
iris_data = load_iris()
feature_1 = iris_data.data[:,0]
feature_3 = iris_data.data[:,2]
plt.scatter(feature_1, feature_3)
plt.show()








 

posted @ 2020-03-12 12:20  Iceberg_710815  阅读(3964)  评论(0编辑  收藏  举报