每日一课 | Python-图表样式

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

添加注释 (Adding Annotations)

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

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

Its output is as follows −

输出如下-

添加图例 (Adding Legends)

有时我们需要绘制带有多条线的图表。图例的使用表示与每一行相关的含义。在下面的图表中,我们有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)

输出如下-

图表展示风格 (Chart presentation 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)

输出如下-

翻译自:

https://www.tutorialspoint.com/python_data_science/python_chart_styling.htm

推荐阅读--

每日一课 | 在Python中获取列表的长度和大小

每日一课 | Python 中生成 0 到 9 之间的随机整数

每日一课 | 如何将int转换为String

每日一课 | Python的分支结构讲解

每日一课 | Python程序将字符串的M个字符重复N次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值