第三方库:pandas、numpy、matplotlit
绘制柱状图
import matplotlib.pyplot as plt
list1 = [1, 4, 6, 3, 7, 8]
list2 = [3, 5, 6, 2, 8, 8]
total_width, n = 0.8, 2
width = total_width / n
y = [2, 4, 6, 8, 10]
x = list(range(len(list1)))
plt.bar(x, list1, width=width)
for i in range(len(x)):
x[i]=x[i]+width
plt.bar(x, list2, width=width)
plt.legend()
plt.show()
绘制饼状图
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame(3 * np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'], columns=['x'])
df.plot.pie(subplots=True)
plt.show()
绘制箱型图
import matplotlib.pyplot as plt
import pandas as pd
# 添加成绩表
plt.style.use("ggplot")
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['font.sans-serif']=['SimHei']
# 新建一个空的DataFrame
df=pd.DataFrame()
df["语文"]=[78,90,97,71,70,93,86,83,78,85,81]
df["概率论"]=[65,95,51,74,99,63,91,82,75,71,55]
df["数据挖掘"]=[93,63,76,88,66,79,83,92,78,86,78]
df["应用统计"]=[85,78,50,95,70,67,82,72,80,81,77]
df
df.boxplot()
plt.show()
绘制正态分布图
import numpy as np
import matplotlib.pyplot as plt
import math
u = 0 # 均值μ
u01 = -2
sig = math.sqrt(0.2) # 标准差δ
x = np.linspace(u - 3 * sig, u + 3 * sig, 50)
y_sig = np.exp(-(x - u) ** 2 / (2 * sig ** 2)) / (math.sqrt(2 * math.pi) * sig)
print(x)
print("=" * 20)
print(y_sig)
plt.plot(x, y_sig, "r-", linewidth=2)
plt.grid(True)
plt.show()