Python数据可视化教程:注释,图例和图表背景的样式实现

2018-09-3009:20:41后端程序开发Comments1,995 views字数 1332阅读模式

python中创建的图表可以通过使用用于制图的库中的某些适当方法进一步设置样式。 在本课中,我们将看到注释,图例和图表背景的实现。 我们将继续使用上一章中的代码并对其进行修改以将这些样式添加到图表中。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

添加注释

很多时候,我们需要通过突出显示图表的特定位置来对图表进行注释。 在下面的示例中,我们通过在这些点上添加注释来指示图表中值的急剧变化。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

import numpy as np 
from matplotlib import pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
z = x ^ 3
t = x ^ 4 
# Labeling the Axes and Title
plt.title("Graph Drawing") 
plt.xlabel("Time") 
plt.ylabel("Distance") 
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry') 
plt.annotate(xy=[4,6], s='Third Entry')

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

Python数据可视化教程:注释,图例和图表背景的样式实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

添加图例说明

有时需要绘制多条线的图表。 图例的使用表示与每条线相关联的含义。 在下面的图表中,我们有3条适当的图例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

import numpy as np 
from matplotlib import pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
z = x ^ 3
t = x ^ 4 
# Labeling the Axes and Title
plt.title("Graph Drawing") 
plt.xlabel("Time") 
plt.ylabel("Distance") 
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry') 
plt.annotate(xy=[4,6], s='Third Entry') 
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4)

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

Python数据可视化教程:注释,图例和图表背景的样式实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

图表演示风格

可以使用style包中的不同方法修改图表的表现风格。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

import numpy as np 
from matplotlib import pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
z = x ^ 3
t = x ^ 4 
# Labeling the Axes and Title
plt.title("Graph Drawing") 
plt.xlabel("Time") 
plt.ylabel("Distance") 
plt.plot(x,y)

#Annotate
plt.annotate(xy=[2,1], s='Second Entry') 
plt.annotate(xy=[4,6], s='Third Entry') 
# Adding Legends
plt.plot(x,z)
plt.plot(x,t)
plt.legend(['Race1', 'Race2','Race3'], loc=4) 

#Style the background
plt.style.use('fast')
plt.plot(x,z)
Python

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

Python数据可视化教程:注释,图例和图表背景的样式实现文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6050.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/6050.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定