首页 > Python资料 博客日记
Python图表样式
2023-07-31 16:31:17Python资料围观221次
Python资料网推荐Python图表样式这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣
在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)
执行上面示例代码,得到以下结果 -
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
上一篇:Python图表属性
下一篇:Python os.lchflags()方法
相关文章
最新发布
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Python pyinstaller打包exe最完整教程
- Anaconda版本和Python版本对应关系(持续更新...)
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj