Python数据科学 专题
您的位置:python > Python数据科学专题 > Python图表样式
Python图表样式
作者:--    发布时间:2019-11-20

在python中创建的图表可以通过使用用于制图的库中的某些适当方法进一步设置样式。 在本课中,我们将看到注释,图例和图表背景的实现。 我们将继续使用上一章中的代码并对其进行修改以将这些样式添加到图表中。

添加注释

很多时候,我们需要通过突出显示图表的特定位置来对图表进行注释。 在下面的示例中,我们通过在这些点上添加注释来指示图表中值的急剧变化。

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')

执行上面示例代码,得到以下结果 -

添加图例说明

有时需要绘制多条线的图表。 图例的使用表示与每条线相关联的含义。 在下面的图表中,我们有3条适当的图例。

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包中的不同方法修改图表的表现风格。

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)

执行上面示例代码,得到以下结果 -


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册