08_添加图例、添加标题(Adding a Title)
8.添加图例
对于前面示例的折线图,我们必须查看代码来了解描述的是哪种函数。 为方便起见,应在绘图中提供此信息。
图例(Legend)常在地图中使用。 Legend用来描述地图的图形语言或符号系统。
Matplotlib可以使用图例来解释图中函数或值的代表的含义。
在下面的简单示例中演示如何在图形上放置图例。 图例包含一个或多个entries。 每个entry都包含一个键(key)和一个标签(label)。
pyplot 函数
legend(*args, **kwargs)
用来在轴上放置一个图例
# next line only needed if working with "ipython notebook":
import numpy as np
import matplotlib.pyplot as plt
ax = plt.gca()
ax.plot([1, 2, 3, 4])
ax.legend(['A simple line'])
plt.show()
如果向plot函数添加一个标签(label),该值将用作legend命令中的标签。 legend函数还需要的参数是位置参数“loc”:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 25, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, '-b', label='sine')
plt.plot(x, y2, '-r', label='cosine')
plt.legend(loc='upper left')
plt.ylim(-1.5, 2.0)
plt.show()import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 25, 1000)
F1 = np.sin(0.5 * X)
F2 = 3 * np.cos(0.8*X)
plt.plot(X, F1, label="$sin(0.5 * x)$")
plt.plot(X, F2, label="$3 sin(x)$")
plt.legend(loc='upper right')
plt.show()
在许多情况下,我们不知道在plot之前结果可能是什么样子。 例如,legend将使线条的重要部分蒙上阴影。 如果不知道数据的显示情况,最好使用’best’作为loc的参数。 Matplotlib将自动尝试为图例找到最佳位置:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(0, 25, 1000)
F1 = np.sin(0.5 * X)
F2 = 3 * np.cos(0.8*X)
plt.plot(X, F1, label="$sin(0.5 * x)$")
plt.plot(X, F2, label="$3 sin(x)$")
plt.legend(loc='best')
plt.show()
再次运行的时候,发现图例可能会到左上角。在以下两个示例中可以看到 loc ='best’能很好地工作:
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-2 * np.pi, 2 * np.pi, 70, endpoint=True)
F1 = np.sin(0.5*X)
F2 = -3 * np.cos(0.8*X)
plt.xticks( [-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$'])
plt.yticks([-3, -1, 0, +1, 3])
plt.plot(X, F1, label="$sin(0.5x)$")
plt.plot(X, F2, label="$-3 cos(0.8x)$")
plt.legend(loc='best')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-2 * np.pi, 2 * np.pi, 70, endpoint=True)
F1 = np.sin(0.5*X)
F2 = 3 * np.cos(0.8*X)
plt.xticks( [-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$'])
plt.yticks([-3, -1, 0, +1, 3])
plt.plot(X, F1, label="$sin(0.5x)$")
plt.plot(X, F2, label="$3 cos(0.8x)$")
plt.legend(loc='best')
plt.show()
8.1.添加标题(Adding a Title)
使用函数pyplot.title(label, fontdict=None, loc=None, pad=None, **kwargs) 可为绘图设置标题。
import matplotlib.pyplot as plt
days = list(range(1,9))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values)
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.title('Change of Celsius Degrees', size=11)
plt.show()
08_添加图例、添加标题(Adding a Title)相关推荐
- matlab给图片加颜色,Matlab中给figure添加图例(legend),标题(title)和颜色(color)...
在Matlab绘图过程中,尤其是需要将多个图绘制在相同的坐标轴中时,通常需要将不同的曲线设置成为不同的颜色.此外,为了直观,还需要给这张图标增添标题和图例.这篇文章展示了在Matlab的绘图窗口(fi ...
- Python使用matplotlib函数subplot可视化多个不同颜色的折线图、为多个子图添加总标题(main title)
Python使用matplotlib函数subplot可视化多个不同颜色的折线图.为多个子图添加总标题(main title) 目录
- Python使用matplotlib函数subplot可视化多个不同颜色的折线图、为多个子图添加总标题(main title)、自定义设置主标题字体类型、字体大小、字体颜色等
Python使用matplotlib函数subplot可视化多个不同颜色的折线图.为多个子图添加总标题(main title).自定义设置主标题字体类型.字体大小.字体颜色等 目录
- python使用matplotlib可视化subplots子图、为subplots添加主标题(main title)、为subplots的每一个子图添加子图标题(subplots title)
python使用matplotlib可视化subplots子图.为subplots添加主标题(main title).为subplots的每一个子图添加子图标题(subplots title) 目录
- R语言ggplot2可视化:为可视化图像添加多行标题(multi line title)并将多行标题居中对齐(center align)
R语言ggplot2可视化:为可视化图像添加多行标题(multi line title)并将多行标题居中对齐(center align) 目录
- R语言散点图可视化:自定义标题和标签、拟合回归线、lowess为散点图添加平滑拟合线、修改散点图中点颜色和点符号、分组散点图、添加图例、pairs可视化散点图矩阵、ggplt2可视化、lattice
R语言散点图可视化:自定义标题和标签.拟合回归线.lowess为散点图添加平滑拟合线.修改散点图中点颜色和点符号.分组散点图.添加图例.pairs可视化散点图矩阵.ggplt2可视化.lattice ...
- matlab 添加子图图案,matplotlib给子图添加图例的方法
matplotlib给子图添加图例的方法 代码如下: import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [5,2,4,2,1,4,5, ...
- html页面导航图标添加,vue 切换网页导航栏添加logo及标题
index.html 网页导航栏添加logo及标题 var scene = window.location.host if (scene === 'anfang.cn') { document.que ...
- plt.scatter散点图添加图例总结
文章目录 全自动方法 改进方法 全自动方法 plt.figure(figsize=[20,20]) scatter=plt.scatter(result[:,0],result[:,1],c=subs ...
最新文章
- 线上java问题排查
- 《陶哲轩实分析》引理17.2.4证明_导数的唯一性
- script 标签中async 属性和defer 属性作用以及区别?
- PTA —— 基础编程题目集 —— 编程题 —— 7-2 然后是几点 (15 分)
- 二级 计算机 vf,计算机二级(VF)笔试-144.doc
- python创_Python创建Windows 服务
- 华为发布7nm制程麒麟810芯片:自研达芬奇架构 nova5首搭
- 【Siddhi】DefinitionNotExistException: Stream with stream ID xxx has not been defined
- 封闭期5年的基金属于什么基金?
- iOS开发学无止境 - 只会左键断点?是时候试试这样那样断点了
- 软件jmeter压力测试实例,jmeter压力测试案例实战
- web通用组件+Axure原型+Axure元件库+Axure后台管理系统框架模板+大屏数据可视化元件库+智慧社区管理系统大屏+图表组件+表单组合+智慧数据看板+通用大屏图表原件库+电脑端常用组件
- 从零开始搭建Salt Web之初探salt-api
- 最优传输论文(二十六):Sliced Wasserstein Discrepancy for Unsupervised Domain Adaptation论文原理
- 2018 软件学院 AK 杯 题解
- ai前世识别_AI人脸识别前世今生app-AI人脸识别前世今生软件下载v2.0-西西软件下载...
- 搞个网站需要多少钱【网站费用】
- python workflow_如何用 Python 写 Alfred Workflow
- 研究者们公布图像识别软件方面的最新进展
- Redis报错:WRONGTYPE Operation against a key holding the wrong kind of value;解决办法
热门文章
- MySQL 之Navicat Premium 12安装使用、pymysql模块使用、sql注入问题的产生与解决
- Python可视化中Matplotlib绘图(2.设置范围、标签、标题、图例(详细参数))
- 二进制蚁群算法【源码实现】
- python天天向上的力量 B
- wxWidgets:wxHtmlCellEvent类用法
- wxWidgets:wxCriticalSectionLocker类用法
- boost::safe_numerics模块实现检测到的数据类型溢出的测试程序
- boost::mpl::abs相关的测试程序
- boost::process::std_err日历相关的测试程序
- boost::mp11::mp_replace_third相关用法的测试程序