Matplotlib实例

from typing import List
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math

from matplotlib.ticker import AutoMinorLocator

#设置画布风格
plt.style.use('seaborn-talk')
#支持显示中文
plt.rcParams['font.sans-serif'] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False

#fig: 画布
fig = plt.figure('First Graph', figsize=(12, 6))
fig.subplots_adjust(wspace=0.4, hspace=0.4)

#axes、ax:创建并选择子图
ax = fig.add_subplot(2,2,1)
ax1 = fig.add_subplot(2,2,2)
ax2 = fig.add_subplot(2,2,3)
ax3 = fig.add_subplot(2,2,4)

#############################################################
# 绘制线
xlst = np.linspace(0, 4*np.pi, 401)
ylst = np.sin(xlst)
ylst1 = np.cos(xlst)
ylst2 = np.cos(xlst+np.pi/4)

# Ax.plot(x, y, color=,linestyle =,linewidth=,marker=,makersize=,label=,markerfacecolor=,markeredgecolor=,markeredgewidth=)
# linestyle:可取'-'、'--'、'-.'、':',默认为'-'
# marker:可取'o'、'.'、','、'x'、'+'、'v'、'^'、'<'、'>'、's'、'd'等20种,默认为None
# color:1)通过颜色简写名称指定:rgbcmyk;2)16进制的RRGGBB值:'#FFDD44'
# 参数的意义:顾名思义
ax.plot(xlst, ylst, linewidth = 1, linestyle='-.', marker='o', markersize = 3, color = 'c')
ax.plot(xlst, ylst1, linewidth = 1, linestyle='--', marker='<', markersize = 2, color = 'k')
ax.plot(xlst, ylst2, linewidth = 1, linestyle=':', marker='x', markersize = 2, color = 'y')

xticks = np.linspace(0, 4, 9)
xticks = xticks*np.pi
ax.set_xticks(xticks)
ax.set_xticklabels(['0pi', '1/2pi', '1pi', '3/2pi', '2pi', '5/2pi', '3pi', '7/2pi', '4pi'])
ax.set_xlabel('弧度')

ax.set_ylabel('三角函数')
ax.set_ylim(-1, 1)

ax.xaxis.set_minor_locator(AutoMinorLocator(2))
# 绘制plt.grid
# linestyle\linewidth:同plot
# axis:x、y、both
ax.grid(b=True, linestyle=':', linewidth=1, axis = 'both')
ax.set_title('三角函数显示')

#############################################################
# 绘制散点图
xlst3 = np.random.random(100)*100
ylst3 = np.random.random(100)*100
colorlist = 'rgbcmyk'
nolst3 = np.random.randint(0, 7, size = 100)
clst3 =[]
for i in nolst3:
    clst3 += [colorlist[i]]
print(clst3)
ax1.scatter(xlst3, ylst3, s=nolst3*10,  c=clst3)

#############################################################
# 绘制柱状图
# [1,3],[2,4][3,5][4,6]
# 特别注意x轴是从整数0开始的,下标要从0开始标;
movelen = 0.2
xarr = np.array([1,2,3,4], dtype=np.int32)
ax2.bar(xarr, [3, 4, 5, 6], width = 0.1, label = '第一组值')
ax2.bar(xarr - movelen, [5,6,7,8], width = 0.1, label = '第二组值')
ax2.bar(xarr + movelen, [9,10,11,12], width = 0.1, label = '第三组值')
ax2.set_xticks(xarr)
ax2.set_xticklabels(['分类1', '分类2', '分类3', '分类4'])
# 图例自动与图表中的label相关联
ax2.legend()

#############################################################
# 绘制直方图
# 直方图是对xarr1里面的数字,根据第二个array/list里面的分隔,自动统计落在每个分隔中的元素的数量
# xarr1 = np.arange(1, 100)
# yarr1 = np.array([0,10,20,30,40,50,60,70,80,90,100],dtype = np.int32)
# ax3.hist(xarr1, yarr1, width=4)
# ax3.xaxis.set_minor_locator(AutoMinorLocator(2))

#############################################################
# 绘制饼图
labels = ['第1季度GPD','第2季度GPD','第3季度GPD','第4季度GPD']
sizes = [30, 20, 25, 25]
explodes = [0.1, 0, 0, 0]

# sizes:各块的占比
# explode:各块离开圆心的比例
# labels:各块的中文标识
# autopct:'%1.2f%%',其中'%1.2f%'指小数点后2位的方式显示,最后的'%'代表数字后加一个'%'
# startangle:指第一个块的起始角度
# radius:代表饼图的半径
ax3.pie(sizes, explode = explodes, labels=labels, \
    autopct='%1.2f%%', shadow=True, startangle = 0, radius = 1.2)

# 保存当前绘图
plt.savefig('f:\\ttt.jpg')
plt.show()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值