python基础之matplotlib

# matplotlib 模块有什么用?
import matplotlib.pyplot as plt
# 只识别英语
from matplotlib.font_manager import FontProperties
%matplotlib inline
#jupyter 默认不显示图片,通过这一行告诉他显示图片
font = FontProperties(fname='D:\msyh.ttc')
# 数据;
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)
# plt.xticks(class_index,classes,FontProperties=font)
plt.bar(class_index,student_amounts)
plt.xticks(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_amount)
    plt.text(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:\msyh.ttc')
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.01510425
 57.57637517]
[102.16850557 100.56996353  93.00436699 ... 109.18668621 105.86546668
 119.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()

在这里插入图片描述

实例

  • https://www.cnblogs.com/nickchen121/p/10807571.html#%E4%BA%8C%E7%9B%B4%E6%96%B9%E5%9B%BE
# 箱装图
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:\msyh.ttc')
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
plt.figure(figsize=(10, 4))
# 创建图表、数据

f = df.boxplot(
    sym='o',  # 异常点形状,参考marker
    vert=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, 均值的横线

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值