python中matplotlib包的基本使用

matplotlib

# Author:NDK
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import datetime
import matplotlib as mpl
import numpy as np
# plt.plot([0,1,2,3,4],[0,-1,-2,-3,-4])
# h = [15,17,21,33,44,55]
# w = [13,16,26,35,54,65]
"""散点图"""
# n = 1000
# h = np.random.randn(n)
# w =h + np.random.randn(n)*0.5
# plt.scatter(h,w,s=5,c='r',alpha=0.5)
# plt.show()

"""折线图"""
# x = np.linspace(-10,10,10)
# y = x**2
# # matplotlib 默认为折线图
# plt.plot(x,y)
# plt.show()

"""条形图"""
# N= 5
# index = np.arange(N)
# y = [20,10,5,25,30]
# pl = plt.bar(left=index,height=y,width=0.5)
# plt.show()

"""直方图"""
# mu =100
# n =20
# x = mu+n*np.random.randn(2000)
# # plt.hist(x,bins=30)  #bins设置分区间个数
# y = np.random.randn(2000)+2
# plt.hist2d(x,y,bins=50)
# plt.show()

"""饼状图"""
# lables = 'A','B','C','D'
# fracs = [20,10,35,26]
# explode = [0,0,0.08,0]
# plt.axes(aspect=1)
# plt.pie(x=fracs,labels=lables,autopct='%.0f%%',explode=explode,shadow=True)
# plt.show()


"""绘制子图"""
# x = np.arange(1,100)
# fig = plt.figure()
# ax1 = fig.add_subplot(221)
# ax1.plot(x,x)
# ax1 = fig.add_subplot(222)
# ax1.plot(x,-x)
# ax1 = fig.add_subplot(223)
# ax1.plot(x,x**2)
# ax1 = fig.add_subplot(224)
# ax1.plot(x,np.log(x))
# plt.show()

"""生成多张图"""
# fig1 = plt.figure()
# ax1 =fig1.add_subplot(111)
# ax1.plot([1,2,3],[3,2,1])
# fig2 = plt.figure()
# ax2 = fig2.add_subplot(111)
# ax2.plot([1,2,3],[3,2,1])
# plt.show()

"""画网格图"""
# x= np.arange(0,10,1)
# fig = plt.figure()
# ax = fig.add_subplot(111)
# ax.plot(x,x*2)
# ax.grid(color='r',linestyle='-.')
# plt.show()

"""画图例"""
# x = np.arange(1,11)
# plt.plot(x,x*2,label='Normal')
# plt.plot(x,x*3,label='Fast')
# plt.plot(x,x*4,label='Faster')
# plt.legend()
# plt.show()

"""坐标轴范围调整"""
# x = np.arange(-10,11)
# plt.plot(x,x**2)   #调整范围
# plt.ylim([0,100])
# plt.xlim([-10,10])

# plt.plot(x,x)
# ax = plt.gca()
# ax.locator_params(nbins=50)    #调整疏密程度
# plt.show()

# start = datetime.datetime(2015,1,1)
# end = datetime.datetime(2016,1,1)
# delta = datetime.timedelta(days=1)
# dates = mpl.dates.drange(start,end,delta)
# y = np.random.rand(len(dates))
# ax = plt.gca()
# ax.plot(dates,y,linestyle='-')
# date_format = mpl.dates.DateFormatter('%Y-%m')
# ax.xaxis.set_major_formatter(date_format)
# # plt.autofmt_xdate()
# plt.show()















































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值