欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python数据可视化之matplotlib实践(1.入门②)

程序员文章站 2022-03-22 16:56:27
...

1.3.8函数annotate()

函数功能:添加图形内容细节的指向型注释文本

函数样例:
plt. plt.annotate(string, xy = (np.pi/2, 1.0),
xytext = ( np.pi/2+1, 0.8 ), weight = ‘bold’, color = ‘y’, arrowprops = dict( arrowstyle = ‘->’, connectionstyle = ‘arc3’, color = ‘b’ ))

参数说明:
string:图形内容注释文本
xy:被注释图形内容的位置坐标
xytext:注释文本的字体颜色
weight:注释文本的字体粗细风格
color:注释文本的颜色
arrowprops:指示箭头的属性字典

代码样例:

#-*-coding: UTF-8 -*-
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x,y,ls = '-.', lw = 2, c = 'c', label = 'plot figure')
plt.legend()
plt.annotate('maximum', 
      xy = (np.pi/2, 1.0), 
      xytext = ( np.pi/2+1, 0.8 ), 
      weight = 'bold',
      color = 'y',
      arrowprops = dict( arrowstyle = '->', connectionstyle = 'arc3', color = 'b' )
      #arrowprops = {'arrowstyle': '->', 'connectionstyle': 'arc3', 'color': 'b' }
      )

运行结果:
Python数据可视化之matplotlib实践(1.入门②)
1.3.9函数text()

函数功能:添加图形内容细节的无指向型注释文本

函数样例:plt.text(x, y, string, weight=’bold’, color=’b’)

参数说明:
x:注释文本内容所在位置横坐标
y:注释文本内容所在位置纵坐标
string:注释文本内容
weight:注释文本内容的粗细风格
color:注释文本内容的颜色字体

代码样例:

#-*-coding: UTF-8 -*-
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls='-.', lw =2, c='c', label='plot figure')
plt.legend()
plt.text(3.1, 0.09, 'y=sin(x)', color = 'b', weight = 'bold')
#plt.text(x,y,string,weight,color)
#无指向型注释文本
plt.show()

运行结果:
Python数据可视化之matplotlib实践(1.入门②)
1.3.10函数title()

函数功能:添加图形标题

函数样例:plt.title(string)

参数说明:
string:图形内容的标题文本

代码样例:

#-*-coding: UTF-8 -*-
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls='-.', lw =2, c='c', label='plot figure')
plt.legend()
plt.title('y=sin(x)')
#添加标题
plt.show()

运行结果:
Python数据可视化之matplotlib实践(1.入门②)
1.3.11函数legend()

函数功能:标示不同图形的文本标签图例

函数样例:plt.legend(loc=’lower left’)

参数说明:
loc:图例在图中的地理位置

代码样例:

#-*-coding: UTF-8 -*-
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls='-.', lw =2, c='c', label='plot figure')
plt.legend(loc='lower right')
#loc:legend窗口的位置
#loc='upper left'
#loc='center'
plt.show()

运行结果:
Python数据可视化之matplotlib实践(1.入门②)
1.4 函数组合应用

将1.3所绘制的重要组成元素的函数放在一起做出一次简单的可视化操作

案例代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Mar  1 08:19:56 2020

@author: 16502
"""

#-*-coding: UTF-8 -*-
import matplotlib.pyplot as plt
import numpy as np
#difine data
x = np.linspace(0.5, 3.5, 100)
y = np.sin(x)
y1 = np.random.randn(100)
plt.scatter(x,y1,c='0.25',label='scatterfigure')
plt.plot(x,y,ls='--',lw=2,label='plot figure')  
for spine in plt.gca().spines.keys():
	print(spine)
	if spine == 'top' or spine == 'right':
		plt.gca().spines[spine].set_color('none')
plt.gca().xaxis.set_ticks_position('bottom') #设置刻度标签
plt.gca().xaxis.set_ticks_position('left')
plt.xlim(0,4)
plt.ylim(-3,3)
plt.ylabel('y_axis')
plt.xlabel('x_axis')
plt.grid(True, ls=':', color='r')
plt.axhline(y=0, c='r', ls='--', lw=2)
plt.axvspan(xmin=1, xmax=2, facecolor='y', alpha=.3)
plt.annotate('maximum', 
      xy = (np.pi/2, 1.0), 
      xytext = ( np.pi/2+0.15, 1.5 ), 
      weight = 'bold',
      color = 'r',
      arrowprops = dict( arrowstyle = '->', connectionstyle = 'arc3', color = 'r' )
)
plt.annotate("", 
      xy = (3.5, -2.98), 
      xytext = ( 3.6, -2.7 ), 
      #weight = 'bold',
      #color = 'r',
      arrowprops = dict( arrowstyle = '->', connectionstyle = 'arc3', color = 'b' )
)
plt.annotate("spines", 
      xy = (0.75, -3), 
      xytext = ( 0.35, -2.25 ), 
      weight = 'bold',
      color = 'b',
      arrowprops = dict( arrowstyle = '->', connectionstyle = 'arc3', color = 'b' )
)
plt.annotate("", 
      xy = (0, -2.78), 
      xytext = ( 0.4, -2.32 ), 
      #weight = 'bold',
      #color = 'b',
      arrowprops = dict( arrowstyle = '->', connectionstyle = 'arc3', color = 'b' )
)
plt.text(3.6, -2.7, "'|' is tickline", weight='bold', color='b')
plt.text(3.6, -2.95, '3.5 is ticklabel', weight='bold', color='b')
plt.title('structure of matplotlib')
plt.legend()
plt.show()

运行结果:
Python数据可视化之matplotlib实践(1.入门②)