Matplotlib-常用图表

使用Matplotlib绘制相关图形
1、折线图
2、散点图/气泡图
3、条形图/柱状图
4、饼图
5、直方图
6、箱线图
7、热力图
知识:
1.绘图前先载入库
import matplotlib.pyplot as plt
2. ax子对象的组件内容
Title 图表标题

plt.title()

Label 坐标轴标注

plt.xlabel()
plt.ylabel()

Tick Label 刻度设置和刻度标注

plt.xticks()
plt.yticks()
例子:
plt.xticks(
[0,1,2,5,8,10], # 刻度设置
[‘2000’,‘2001’,‘2002’,‘2005’,‘2008’,‘2010’], # 刻度标注
fontsize = 14, # 文字大小
rotation = 90, # 旋转角度
)

Axis 坐标范围

plt.axis() # ,x轴,y轴
plt.xlim() # x轴坐标范围
plt.ylim() # y轴坐标范围
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
n1, = plt.plot([1,3,5,7,9],[0,4,2,8,6], label = ‘number2图例标题1’)
n2,= plt.plot([1,3,5,7,9],[4,2,8,6,10], label = ‘number2图例标题2’)
plt.legend(
handles = [n1, n2], # 给指定数据绘制图例
loc = ‘upper right’, # 图例位置
frameon= False, # 有无边框
prop=font_set
)
#网格
plt.grid(linewidth = 0.2)

1、折线图

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt 
index_inform=pd.read_excel('hushi.xlsx', None)['Sheet2']
plt.plot(
    index_inform['date'],  # x轴数据,日期
    index_inform['index_hu'],  # y轴数据,收盘价
    color='r',  # 线条颜色
    linestyle='--',  # 线条风格
    linewidth=2,  # 线条粗细
    marker='D',  # 标记风格
    markerfacecolor='#ffff00',  # 标记颜色
    markersize=6,  # 标记大小
    alpha=0.5,  # 透明度
    label='index_hu',
)
plt.legend()  # 让图例生效
plt.xticks([5,10,15,20,25])#设置x轴的坐标
plt.yticks([2800,2900,3000])#设置x轴的坐标
plt.margins(0)#设置图形离边框的距离
plt.subplots_adjust(bottom=0.1)
plt.xlabel('date') #X轴标签
plt.ylabel("index_hu") #Y轴标签
plt.title("A index_hu plot") #标题
plt.savefig('D:\\hu.jpg',dpi = 800)

在这里插入图片描述

2、散点图/气泡图(scatter)

plt.scatter(
    index_inform['date'],
    index_inform['index_hu'], 
    #s = index_inform['index_shen'], 
    c = 'r',
    alpha=0.5,
    linewidths=1, 
    marker='o',
    edgecolors='k' 
)

在这里插入图片描述

3,条形图/柱状图

bar柱状图barh条形图

#绘图,上证指数
plt.bar(
    index_inform['date'][:4], # x轴,时间
    index_inform['index_hu'][:4],  # y轴,上证指数
    color = 'b',  # 颜色
    width = 0.3,
    alpha = 0.3,
)
#绘图,女生
#科目坐标
plt.bar(
    index_inform['date'][:4]+0.3, # x轴,时间加0.3
    index_inform['index_shen'][:4],  # y轴,深证成指
    color = 'r',  # 颜色
    width = 0.3,
    alpha = 0.3,
)
#时间坐标数值替换为字符
plt.xticks(index_inform['date'][:4]+0.15,[3,4,5,6])
for i,j in zip(index_inform['date'][:4],index_inform['index_hu'][:4]):
    plt.text(
        i,  # x轴,时间位置
        j,  # y轴,上证指数
        '%.1f' % j,  # 数据转为一位小数
        ha='center', # 水平对齐
        va='bottom', # 垂直对齐
        alpha = 0.5,
    )

for i,j in zip(index_inform['date'][:4]+0.3,index_inform['index_shen'][:4]):
    plt.text(
        i,
        j,
        '%.1f' % j,
        ha='center',
        va='bottom',
        alpha = 0.5,
    )

在这里插入图片描述

4.饼图

#绘图,上证指数
#国名
mark = ['America','China','India','Saudi','Russia','Japan','Britain','Germany','France']
#各国占9国总军费的比例
percent = [0.5548467,0.14444868,0.05094268,0.04846696,0.046753,0.04418206,0.04161112,0.03799276,0.03075605]
plt.pie(
    percent,  # 百分比
    labels = mark,  # 名称
    explode=(0,0.1,0,0,0,0,0,0,0),  # 突出块,突出比例
    autopct='%1.1f%%',  # 显示百分比方式
    shadow=True,  # 阴影效果
    startangle=-100,  # 饼图起始的角度,度数,默认0为右侧水平180度开始,逆时针旋转
)
plt.axis('equal') #正圆形饼图,x/y轴尺寸相等.默认是扁图

在这里插入图片描述

5.直方图

plt.hist(index_inform['index_hu'],10,normed=1,histtype='bar',facecolor='yellowgreen',alpha=0.75)
plt.hist(index_inform['index_shen'],10,normed=1,histtype='bar',alpha = 0.75)

在这里插入图片描述

6.箱线图

最大非异常值,上边线
Q3,箱体上边缘上四分位数
Q2,中位数线
Q1,箱体下边缘下四分位数
最小非异常值,下边线

plt.boxplot(
        index_inform['index_hu'],
    #(index_inform['index_hu'],index_inform['index_shen']),  # 数据
    #labels = ('hu','shen'),  # 标签
    showfliers = True,  # 是否显示异常值,默认显示
    whis = 1.5,  # 指定异常值参数:默认1.5倍四分位差
    showmeans = True, # 是否显示平均值,默认不显示
    meanline = True, # 是否用线标示平均值,默认用点
    widths = 0.5, # 柱子宽度
    vert =False, # 默认True纵向,False横向
    patch_artist = True,  # 是否填充颜色
    boxprops = {'facecolor':'#ffff00','color':'green'}, # 箱体样式
)
plt.grid(linewidth=0.2)

在这里插入图片描述

7.热力图

plt.imshow(
    a,  # 数据
    cmap='gray',  # 配色,gray灰度
    origin='lower', # 水平翻转,默认upper,lower
    interpolation='lanczos', # 渲染,模糊
)

plt.colorbar() #侧栏

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值