莫烦Matplotlib可视化第二章基本使用代码学习

基本用法

import matplotlib.pyplot as plt
import numpy as np

"""
2.1基本用法
"""
# x = np.linspace(-1,1,50)    #[-1,1]50个点
# #y = 2*x +1
#
# y = x**2
# plt.plot(x,y)   #注意:x,y顺序不能反
# plt.show()

"""
2.2figure图像
"""

# x = np.linspace(-3,3,50)
# y1 = 2*x +1
# y2 = x**2
# # plt.figure()    #一个figure对应一张图
# # plt.plot(x,y1)
#
# plt.figure(num=3,figsize=(8,5))
# #num可以改变这是第几个figure,不然默认顺序排列。figsize是设置输出的长宽
# plt.plot(x,y2)
# plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--')
# #线条的颜色,粗细,线条格式
# plt.show()

"""
2.3,2.4 坐标轴设置
"""

# x = np.linspace(-3,3,50)
# y1 = 2*x +1
# y2 = x**2
# plt.figure(num=3,figsize=(8,5))
# plt.plot(x,y2)
# plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--')
#
# plt.xlim((-1,2)) #设置坐标轴范围
# plt.ylim((-2,3))
#
# plt.xlabel('I am x')    #坐标轴描述
# plt.ylabel('I am y')
#
# #2.3
#
# new_ticks = np.linspace(-1,2,5)
# print(new_ticks)
# plt.xticks(new_ticks)   #替换x轴坐标
# plt.yticks([-2,-1.8,-1,1.22,3],
#            [r'$really\ bad$',r'$bad$',r'$normal$',r'$good$',r'$really\ good$'])  #替换y轴坐标为文字
# #r是正则,$是为了让字体变好看,在字体中空格可能不识别,需要\+空格 来得到空格

#2.4

# #gca = 'get current axis'
# ax = plt.gca()  #获得当前坐标轴
# ax.spines['right'].set_color('none')    #spines对应的是图表的边框,right就是右边框,set_color('none')就是使右边框消失
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')   #用下面(bottom)坐标轴代替x
# ax.yaxis.set_ticks_position('left')
# ax.spines['bottom'].set_position(('data',-1))   #把x轴绑定在y轴的-1位置
# ax.spines['left'].set_position(('data',0))
#
# plt.show()

"""
2.5 Legend图例
"""

# l1,=plt.plot(x,y2,label = 'up')
# #plt.plot其实是有返回值的,为了放入handles中必须l1+逗号
# l2,=plt.plot(x,y1,color = 'red',linewidth=1.0,linestyle='--',label = 'down')
# #plt.legend()    #打出来label和图例
# #个性的图例
# plt.legend(handles=[l1,l2], labels =['aaa','bbb'],loc = 'best')    #打出来label和图例
# # loc=best可以自动找数据比较少的地方,防止挡住数据
# plt.show()

"""
2.6 Annotation标注
"""
#
# x = np.linspace(-3,3,50)
# y = 2*x +1
# plt.figure(num=1,figsize=(8,5))
# plt.plot(x,y)
# ax = plt.gca()
# ax.spines['right'].set_color('none')
# ax.spines['top'].set_color('none')
# ax.xaxis.set_ticks_position('bottom')
# ax.yaxis.set_ticks_position('left')
# ax.spines['bottom'].set_position(('data',0))
# ax.spines['left'].set_position(('data',0))
#
# x0 = 1
# y0 = 2*x0 + 1
# plt.scatter(x0,y0,s=50,color = 'b')  #展示(x0,y0)点 ,s = size
# plt.plot([x0,x0],[y0,0],'k--',lw = 2.5) #从[x0,x0]到[y0,0]画条直线,k=black,--是虚线,lw是粗细
#
# #method 1
# plt.annotate(r'$2x+1=%s$'%y0,xy=(x0,y0),xycoords = 'data',xytext=(+30,-30),textcoords='offset points',
#              fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle = 'arc3,rad = .2'))
# #r'$2x+1=%s$'%y0是指标注的文字,xy=(x0,y0)是指出这个点,xycoords以data值为基准,textcoords是指在xytext的位置上(x+30,y-30)进行描述
#
# #method 2
# plt.text(-3.7,3,r'$This\ is\ the\ some\ text.\mu\ \sigma_i\ \alpha_t$',fontdict={'size':16,'color':'r'})
# plt.show()

"""
2.7 tick能见度
"""

x = np.linspace(-3,3,50)
y = 0.1*x

plt.figure()
plt.plot(x,y,linewidth = 10)
plt.ylim(-2,2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(12)
    label.set_bbox(dict(facecolor= 'white',edgecolor='None',alpha = 0.7))  #标注颜色,标注背景颜色,alpha是透明度

plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值