9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出
9.添加标注 (Annotations)
10.添加网格线(Grid Lines)
11.显示中文字体
12.保存图形(saving Figures)
13.高质量矢量图输出
9.添加标注 (Annotations)
有时候我们对某点如3∗sin(3∗pi/4)的值特别感兴趣。
import numpy as np
print(3 * np.sin(3 * np.pi / 4))
2.121320343559643
如果想在图标上标出这一点,可以使用annotate函数执行此操作。
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-2 * np.pi, 3 * np.pi, 70, endpoint=True)
F1 = np.sin(X)
F2 = 3 * np.sin(X)
ax = plt.gca()
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])
x = 3 * np.pi / 4
plt.scatter([x,],[3 * np.sin(x),], 50, color ='blue')
plt.annotate(r'$(3\sin(\frac{3\pi}{4}),\frac{3}{\sqrt{2}})$',xy=(x, 3 * np.sin(x)),xycoords='data',xytext=(+20, +20),textcoords='offset points',fontsize=16,arrowprops=dict(facecolor='blue'))
plt.plot(X, F1, label="$sin(x)$")
plt.plot(X, F2, label="$3 sin(x)$")
plt.legend(loc='lower left')
plt.show()
我们必须提供有关annotate参数的一些信息。
参数 | 含义 |
---|---|
xy | coordinates of the arrow tip |
xytext | coordinates of the text location |
我们示例的xy和xytext位置在数据坐标中。 我们还可以选择其他坐标系系统。 可以为xy和xytext的坐标系指定字符串值,赋值给xycoords和textcoords。 默认值为“data”:
字符串值 | 坐标系统 |
---|---|
‘figure points’ | Points from the lower left of the figure |
‘figure pixels’ | Pixels from the lower left of the figure |
‘figure fraction’ | Fraction of figure from lower left |
‘axes points’ | Points from lower left corner of axes |
‘axes pixels’ | Pixels from lower left corner of axes |
‘axes fraction’ | Fraction of axes from lower left |
‘data’ | Use the coordinate system of the object being annotated (default) |
‘polar’ | (theta, r) if not native ‘data’ coordinates |
此外,还可以指定箭头的属性。 为此,我们必须为参数arrowprops提供一个箭头属性字典:
arrowprops key | 描述 |
---|---|
width | The width of the arrow in points |
headwidth | The width of the base of the arrow head in points |
headlength | The length of the arrow head in points |
shrink | Fraction of total length to shrink from both ends |
**kwargs | any key for matplotlib.patches.Polygon, e.g., facecolor |
在以下示例中,我们将更改上一示例的箭头的外观:
import numpy as np
import matplotlib.pyplot as pltX = np.linspace(-2 * np.pi, 3 * np.pi, 70, endpoint=True)
F1 = np.sin(X)
F2 = 3 * np.sin(X)
ax = plt.gca()
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])
x = 3 * np.pi / 4
plt.scatter([x, ], [3 * np.sin(x), ], 50, color='blue')
plt.annotate(r'$(3\sin(\frac{3\pi}{4}),\frac{3}{\sqrt{2}})$',xy=(x, 3 * np.sin(x)),xycoords='data',xytext=(+20, +20),textcoords='offset points',fontsize=16,arrowprops=dict(facecolor='blue', headwidth=10, headlength=10, width=2, shrink=0.1))
plt.plot(X, F1, label="$sin(x)$")
plt.plot(X, F2, label="$3 sin(x)$")
plt.legend(loc='lower left')
plt.show()
10.添加网格线(Grid Lines)
import numpy as np
import matplotlib.pyplot as pltdef f(t):return np.exp(-t) * np.cos(2 * np.pi * t)def g(t):return np.sin(t) * np.cos(1 / (t + 0.1))t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.plot(t1, g(t1), 'ro', t2, f(t2), 'k')
plt.grid(color='b', alpha=0.5, linestyle='dashed', linewidth=1.0)
plt.show()
11.显示中文字体
Matplotlib默认是不支持显示中文字符的。
解决方法: 可以使用 rc 配置(rcParams)来自定义图形的各种默认属性。
Windows操作系统支持的中文字体和代码:
配置方式:
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
示例如下:
import matplotlib.pyplot as plt
# 在jupyter notebook 中,设置下面两行来显示中文
plt.rcParams['font.family'] = ['sans-serif']
# 在 PyCharm 中,只需要下面一行,‘STXingkai’:华文行楷
plt.rcParams['font.sans-serif'] = ['STXingkai']
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('日期', size=16)
plt.ylabel('摄氏度', size=16)
plt.title('温度变化', size=16)
plt.show()
另外的方法:
在每一处使用到中文输出的地方,都加上一个字体属性fontproperties
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('日期', fontproperties='SimHei', size=16)
plt.ylabel('摄氏度', fontproperties='STXingkai', size=16)
plt.title('温度变化', fontproperties='SimHei', size=16)
plt.show()
12.保存图形(saving Figures)
savefig方法可用来保存图形到文件中:
fig.savefig(“filename.png”)
可以指定分辨率DPI(Dots Per Inch,每英寸点数)和选择输出文件格式:
可以采用PNG,JPG,EPS,SVG,PGF和PDF格式生成输出。
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot([0, 1, 2, 3, 4], [0, 3, 5, 9, 11])
plt.xlabel('Months')
plt.ylabel('Books Read')
plt.show()
fig.savefig('books_read.png')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('books_read.png')
plt.imshow(img)
13.高质量矢量图输出
Jupyter Notebook中显示svg矢量图的设置: %config InlineBackend.figure_format = ‘svg’
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
# 在jupyter notebook 中,设置下面两行来显示中文
plt.rcParams['font.family'] = ['sans-serif']
# 在 PyCharm 中,只需要下面一行
plt.rcParams['font.sans-serif'] = ['SimHei']
days = list(range(1,9))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
fig = plt.figure()
plt.plot(days, celsius_values)
plt.xlabel('日期', size=16)
plt.ylabel('摄氏度', size=16)
plt.title('温度变化', size=16)
plt.show()
fig.savefig('celsius_degrees.svg')
9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出相关推荐
- python 整数输出 d f_如何将数字(10,11,12,13,14,15)分配给Python 3中的字母(A,B,C,D,E,F)?...
您可以在代码中添加更多行来执行此操作: 首先创建两个带有字符的列表,一个带有要映射的整数,然后从那些创建dict: list_1=["A","B"," ...
- atitit.Oracle 9 10 11 12新特性attilax总结
atitit.Oracle 9 10 11 12新特性 1. ORACLE 11G新特性 1 1.1. oracle11G新特性 1 1.2. 审计 1 1.3. 1. 审计简介 1 1.4. ...
- 高通平台(Qualcomm) Android 10 /11/12 user版本默认打开adb 调试小结
1.流程 USB插入接收广播授权->建立adb连接服务->执行adb 命令 2.adb相关属性 ro.secure = 0 开启root权限 ro.adb.secure = 1 1开启ad ...
- 2021-03-08~09~10~11~12 大数据课程笔记 day47day48day49day50day51
@R星校长 大数据技术之Flink 第一章 初识Flink 在当前数据量激增的时代,各种业务场景都有大量的业务数据产生,对于这些不断产生的数据应该如何进行有效的处理,成为当下大多数公司所面临的问题 ...
- 鸟哥的Linux私房菜-第10/11/12/13章(vim程序编辑器、学习bash、正则表达式与文件格式化处理、学习Shell Scripts)...
第10章 vim程序编辑器 可以将vim看做vi的进阶版本,vim可以用颜色或底线等方式来显示出一些特殊的信息. 为何要学习vim?因为: a. 所有的 Unix Like 系统都会内建 vi 文书编 ...
- 【传智播客】Javaweb程序设计任务教程 黑马程序员 第7,8,9,10,11,12,13,14,15章 课后答案
所有章节答案合集-->传送门 第一部分是题目,向下翻, 第二部分是答案 第七章 [测一测] 学习完前面的内容,下面来动手测一测吧,请思考以下问题: 1.简述Javabean规范都有哪些?(至少写 ...
- Ubuntu 16G内存在android系统 10/11/12/13编译 framework时候经常oom内存不足,电脑卡死解决办法stubs-docs-non-updatable metalava
hi粉丝朋友们: 在android高版本编译时候经常会遇到framework一个相关任务编译时候经常把内存吃满: //frameworks/base:system-api-stubs-docs-non ...
- 构建之法10,11,12章的读后感
第十章:典型用户和场景 问题 :什么是典型用户? 第十一章:软件设计与实现 问题 :开发人员的标准工作流程就是不断的发现BUg,修改bug来完善功能,在此过程中要等待同伴复审,在这阶段中,开发者应该如 ...
- jdk 8 、9 10 11 12 13 14和 jdk 1.8 什么关系??
五.jdk 13 和 jdk 1.8 什么关系 jdk 8 就是 jdk 1.8 jdk9 就是 jdk 1.9 其他同理 官方虽然更新的快,但是大多数公司,为因为习惯问题和调整的麻烦,在加上 jdk ...
最新文章
- Deploying Windows Mobile 6 with Exchange Server 2007 白皮书
- cmake的使用--目标的编译附源码
- 最前线|阿里大文娱板块Q3营收同比增长20%,优酷及UC业务带动显著
- 图论--LCA--在线RMQ ST
- mysql slave 1032_修复mysql slave复制1032错误
- django-form and fields validation
- linux访问windows共享目录开机自动挂载
- 集成新版(5.17+)Activiti Modeler与Rest服务
- python3 tkinter 实现凯撒密码GUI界面
- 【JAVA笔记——道】Hadoop设计模式--抽象类
- ZendFramework-2.4 源代码 - 整体架构(类图)
- App动态修改图标包名
- 利用Lightroom添加边框及批量导出
- 《地理天机一贯 》 聚宝馆手抄珍稀古籍分享
- 怎样为Windows7系统设置快速启动栏
- 副驾驶的意义_副驾驶位置有什么含义?
- 针对Google注册不能收到手机验证码的解决方案
- 基于 Springboot 和 Mybatis 的后台管理系统 BootDo | 软件推介
- hduoj 1874 畅通工程续(Floyd)
- 为什么说 5G 是物联网的时代?
热门文章
- Django框架(16.Django中的模型类管理器以及自定义管理器)
- 电气备自投笔记书籍介绍(持续更新中)
- VTK:隐式布尔值demo用法实战
- boost::static_assert简单的测试程序
- boost::boost::stoer_wagner_min_cut用法的测试程序
- boost::is_straight_line_drawing用法的测试程序
- boost::graph模块实现资源受限最短路径算法的示例使用
- Boost.Geometry 结合 Boost.Graph,计算最短路线
- boost::gregorian模块实现自年初以来的天数的测试程序
- boost::core::bit_cast的测试程序