# matplotlib 模块有什么用?
import matplotlib.pyplot as plt
# 只识别英语
from matplotlib.font_manager import FontProperties
%matplotlib inline
#jupyter 默认不显示图片,通过这一行告诉他显示图片
font = FontProperties(fname='D:')
# 数据;
classes = ['1班','2班','3班','4班']
student_amounts = [20, 30, 40, 50]
class_index = range(len(classes))
# [0,1,2,3] - >[20,30,40,50]
print(class_index)
range(0, 4)
# plt.bar(class_index,student_amounts)
# icks(class_index,classes,FontProperties=font)
plt.bar(class_index,student_amounts)
icks(class_index,classes,fontproperties=font) #这里一定不要大写 不然
plt.xlabel('班级',fontproperties=font)
plt.title('班级-学生人数',fontproperties=font)
plt.ylabel('学生人数',fontproperties=font)for ind,students_amount in enumerate(student_amounts):print(ind,students_(ind,students_amount+1,students_amount) # (x,y,s) -> (x坐标,y坐标,s文本)plt.show()
0 20
1 30
2 40
3 50
# import matplotlib.font_manager as fm
# f1 = fm.FontProperties('simhei', size=20)
# f2 = fm.FontProperties(fname='方正卡通简体.ttf', size=30)
# 直方图
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='D:')
mu1, mu2, sigma = 50, 100, 10
# 构造均值为50的符合正态分布的数据
x1 = mu1 + sigma * np.random.randn(10000)
print(x1)
# 构造均值为100的符合正态分布的数据
x2 = mu2 + sigma * np.random.randn(10000)
print(x2)
# ptl.hist(x1,bins=50) # bins代表柱子的数量
# ptl.hist(x2,bins=50) # bins代表柱子的数量
# fig = plt.figure()# 生成空白画布
# fig.show()
[54.72160072 50.37226964 61.37019565 ... 35.7235959 53.0151042557.57637517]
[102.16850557 100.56996353 93.00436699 ... 109.18668621 105.86546668119.78452712]
fig = plt.figure()
# 背景
plt.style.use('ggplot')
ax1 = fig.add_subplot(121)
# bins=50表示每个变量的值分成50份,即会有50根柱子
ax1.hist(x1, bins=50, color='darkgreen')ax2 = fig.add_subplot(122)
# # 121->1行2列拿第一个
# ax1 = fig.add_subplot(121)
ax2.hist(x2, bins=50, color='orange')
# 大标题 suptitle bold加粗
fig.suptitle('两个正态分布', fontproperties=font, fontweight='bold', fontsize=15)
ax1.set_title('绿色的正态分布', fontproperties=font)
ax2.set_title('橙色的正态分布', fontproperties=font)
plt.show()
# 折线图
from numpy.random import randn
# 修改背景为条纹
plt.style.use('ggplot')np.random.seed(1)# 使用numpy的累加和,保证数据取值范围不会在(0,1)内波动
plot_data1 = randn(40).cumsum() # list
print(plot_data1)
plt.plot(plot_data1)
plt.show()
[ 1.62434536 1.01258895 0.4844172 -0.58855142 0.2768562 -2.02468249-0.27987073 -1.04107763 -0.72203853 -0.97140891 0.49069903 -1.56944168-1.89185888 -2.27591324 -1.1421438 -2.24203506 -2.41446327 -3.29232169-3.25010794 -2.66729273 -3.76791191 -2.6231882 -1.72159748 -1.21910314-0.31824719 -1.00197505 -1.12486527 -2.06063471 -2.32852279 -1.79816732-2.48982807 -2.8865816 -3.5737543 -4.41895994 -5.09020607 -5.10287067-6.22018102 -5.98576532 -4.32596314 -3.58391898]
plot_data2 = randn(40).cumsum()
plot_data3 = randn(40).cumsum()
plot_data4 = randn(40).cumsum()plt.plot(plot_data1, marker='o', color='red', linestyle='-', label='红实线')
plt.plot(plot_data2, marker='x', color='orange', linestyle='--', label='橙虚线')
plt.plot(plot_data3, marker='*', color='yellow', linestyle='-.', label='黄点线')
plt.plot(plot_data4, marker='s', color='green', linestyle=':', label='绿点图')# loc='best'给label自动选择最好的位置
plt.legend(loc='best', prop=font)
plt.show()
# 箱装图
import numpy as np
import pandas as pd
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
font = FontProperties(fname='D:')
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
plt.figure(figsize=(10, 4))
# 创建图表、数据f = df.boxplot(sym='o', # 异常点形状,参考markervert=True, # 是否垂直whis=1.5, # IQR,默认1.5,也可以设置区间比如[5,95],代表强制上下边缘为数据95%和5%位置patch_artist=True, # 上下四分位框内是否填充,True为填充meanline=False,showmeans=True, # 是否有均值线及其形状showbox=True, # 是否显示箱线showcaps=True, # 是否显示边缘线showfliers=True, # 是否显示异常值notch=False, # 中间箱体是否缺口return_type='dict' # 返回类型为字典
)
plt.title('boxplot')for box in f['boxes']:box.set(color='b', linewidth=1) # 箱体边框颜色box.set(facecolor='b', alpha=0.5) # 箱体内部填充颜色
for whisker in f['whiskers']:whisker.set(color='k', linewidth=0.5, linestyle='-')
for cap in f['caps']:cap.set(color='gray', linewidth=2)
for median in f['medians']:median.set(color='DarkBlue', linewidth=2)
for flier in f['fliers']:flier.set(marker='o', color='y', alpha=0.5)
# boxes, 箱线
# medians, 中位值的横线,
# whiskers, 从box到error bar之间的竖线.
# fliers, 异常值
# caps, error bar横线
# means, 均值的横线
本文发布于:2024-01-29 08:45:51,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170648915614089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |