加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python——绘图

发布时间:2020-12-20 10:14:48 所属栏目:Python 来源:网络整理
导读:1、保存图片。 fig.savefig 一、创建画布 1、创建画布和坐标轴 在Matplotlib中,plt.figure类可以看做一个能够容纳各种坐标轴、图形、文字和标签的容器。plt.Axes类是一个带有刻度和标签的矩形,最终会包含所有可视化的图形元素。 此处,fig代表一个图例,ax

1、保存图片。

fig.savefig

一、创建画布

1、创建画布和坐标轴

在Matplotlib中,plt.figure类可以看做一个能够容纳各种坐标轴、图形、文字和标签的容器。plt.Axes类是一个带有刻度和标签的矩形,最终会包含所有可视化的图形元素。

此处,fig代表一个图例,ax表示一个坐标轴实例或一组坐标轴实例。

%matplotlib inline

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()

x = np.linspace(0,10,1000)
ax.plot(x,np.sin(x))

?

2、面向对象风格接口

#面向对象的风格
#创建图形网络
#ax是一个包含两个Axes对象的数组,即有两个坐标轴
fig,ax = plt.subplots(2)

#在每个Axes对象上调用的plot()方法,分别绘制sin()和cos()
ax[0].plot(x,np.sin(x)) 
ax[1].plot(x,np.cos(x))

?

3、Matlab风格接口

plt.figure()#创建图形

#Matlib风格接口
#创建两个子图中的第一个,设置坐标轴,等于fig,ax=plt.subplot()
plt.subplot(2,1,1)  
plt.plot(x,np.sin(x))

#创建两个子图中的第一个,设置坐标轴
plt.subplot(2,2)
plt.plot(x,np.cos(x))

?

 

?

?

二、坐标轴和线条调整

1、调整线条颜色和样式

?

线条样式:

‘-‘?????? solid line style
‘--‘????? dashed line style
‘-.‘????? dash-dot line style
‘:‘?????? dotted line style

?

颜色:

‘.‘?????? point marker
‘,‘?????? pixel marker
‘o‘?????? circle marker
‘v‘?????? triangle_down marker
‘^‘?????? triangle_up marker
‘<‘?????? triangle_left marker
‘>‘?????? triangle_right marker
‘1‘?????? tri_down marker
‘2‘?????? tri_up marker
‘3‘?????? tri_left marker
‘4‘?????? tri_right marker
‘s‘?????? square marker
‘p‘?????? pentagon marker
‘*‘?????? star marker
‘h‘?????? hexagon1 marker
‘H‘?????? hexagon2 marker
‘+‘?????? plus marker
‘x‘?????? x marker
‘D‘?????? diamond marker
‘d‘?????? thin_diamond marker
‘|‘?????? vline marker
‘_‘?????? hline marker

?

plt.axhline(y=1,ls=‘.‘,c-‘yellow‘)#增加水平线

plt.axvline(x=1,ls=‘-‘,c=‘red‘) #增加垂直线

?

2、调整坐标轴

(1)调整坐标轴上下限:

plt.xlim()? #等价于ax.set_xlim()

plt.ylim()? #等价于ax.set_ylim()

plt.axis([xmin,xmax,ymin,ymax])

(2)设置图形标签

plt.title()? #设置图形标题,等价于ax.set_title()

plt.xlabel(),plt.ylabel()? #设置X,Y轴标题,等价于ax.set_xlabel(),ax.set_ylabel()

(3)配置图例

plt.legend()? #创建图例

ax.legend(frameon=False,loc=‘epper left‘)

#选择图例显示的元素
#方式一
plt.legend(lines[:2],[first,second])

#方式二
plt.plot(x,y[:,0],label=frist)
plt.plot(x,1],label=second)
plt.plot(x,2:])
plt.legend(gramealpha=1,frameon=True)#默认情况下会忽略那些不带标签的元素

?

三、多子图

1、图中图

plt.axes([bottom,left,width,height] #[底坐标,坐坐标,宽度,高度]

#xample1
ax1 = plt.axes()
ax2 = plt.axes([0.65,0.65,0.2,0.2])

#example2
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.5,0.8,0.4],xticklabels=[],ylim=(-1.2,1.2))
ax2 = fig.add_axes([0.1,0.1,ylim=(-1.2,1.2))
x = np.linspace(0,10)
ax1.plot(np.sin(x))
ax2.plot(np.cos(x))

2、简易网格子图

plt.subplot(行数,列数,索引值)

for i in range(1,7):
  plt.subplot(2,3,i)
  plt.text(0.5,str((2,i)),
      fontsizt=18,ha=‘center‘)

fig = plt.figure() #plt.subplot_adjust可以调整子图之间的间隔 fig.subplots_adjust(hspace=0.4,wspace=0.4) for i in range(1,7): ax=fig.add_subplot(2,i)
   ax.text(0.5,
      fontstze=18,ha=‘center‘)

?等价于

plt.subplots(2,sharex=col,sharey=row)

#比较subplot & subplots

#subplots_addjust

?

四、文字与注释

ax.text():文字注释

ax.transData:用x轴与y轴标签作为数据坐标
ax.transAxes:以坐标轴左下角为原点,按照坐标轴尺寸的比例呈现坐标.
fig.transFigure:以图形左下角为原点,按照图形尺寸的比例呈现坐标。

fig,ax = plt.subplots(facecolor=lightgray)
ax.axis([0,10,10])

ax.text(1,5,".data:(1,5)",transform=ax.transData)
ax.text(0.5,".Axes:(0.5,0.2)",transform=ax.transAxes)
ax.text(0.5,".Figure:(0.5,transform=fig.transFigure)

plt.annotate():创建箭头

?

参考:《Python数据科学手册》

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读