当前位置:网站首页>【無標題】
【無標題】
2022-06-29 02:25:00 【郭慶汝】
matplotlib matplotlib中在圖像中添加注釋的用法——annotate

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# 繪制一個餘弦曲線
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)
# 繪制一個黑色,兩端縮進的箭頭
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
xycoords='data',
arrowprops=dict(facecolor='black', shrink=0.05)
)
ax.set_ylim(-2, 2)
plt.show()

import numpy as np
import matplotlib.pyplot as plt
# 以步長0.005繪制一個曲線
x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)
# 被注釋點的數據軸坐標和所在的像素
xdata, ydata = 5, 0
xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata))
# 設置注釋文本的樣式和箭頭的樣式
bbox = dict(boxstyle="round", fc="0.8")
arrowprops = dict(
arrowstyle = "->",
connectionstyle = "angle,angleA=0,angleB=90,rad=10")
# 設置偏移量
offset = 72
# xycoords默認為'data'數據軸坐標,對坐標點(5,0)添加注釋
# 注釋文本參考被注釋點設置偏移量,向左2*72points,向上72points
ax.annotate('data = (%.1f, %.1f)'%(xdata, ydata),
(xdata, ydata), xytext=(-2*offset, offset), textcoords='offset points',
bbox=bbox, arrowprops=arrowprops)
# xycoords以繪圖區左下角為參考,單比特為像素
# 注釋文本參考被注釋點設置偏移量,向右0.5*72points,向下72points
disp = ax.annotate('display = (%.1f, %.1f)'%(xdisplay, ydisplay),
(xdisplay, ydisplay), xytext=(0.5*offset, -offset),
xycoords='figure pixels',
textcoords='offset points',
bbox=bbox, arrowprops=arrowprops)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
# 繪制一個極地坐標,再以0.001為步長,畫一條螺旋曲線
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
r = np.arange(0,1,0.001)
theta = 2 * 2*np.pi * r
line, = ax.plot(theta, r, color='#ee8d18', lw=3)
# 對索引為800處畫一個圓點,並做注釋
ind = 800
thisr, thistheta = r[ind], theta[ind]
ax.plot([thistheta], [thisr], 'o')
ax.annotate('a polar annotation',
xy=(thistheta, thisr), # 被注釋點遵循極坐標系,坐標為角度和半徑
xytext=(0.05, 0.05), # 注釋文本放在繪圖區的0.05百分比處
textcoords='figure fraction',
arrowprops=dict(facecolor='black', shrink=0.05),# 箭頭線為黑色,兩端縮進5%
horizontalalignment='left',# 注釋文本的左端和低端對齊到指定比特置
verticalalignment='bottom',
)
plt.show()

import matplotlib.pyplot as plt
# 設置繪圖區標題
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
# 設置子繪圖區標題
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
# 設置x y坐標軸的標識
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
# 紅色、透明度0.5、邊框留白10
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
bbox={
'facecolor':'red', 'alpha':0.5, 'pad':10})
# 文字中有數學公式
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
# 文字中有ASCII碼
ax.text(3, 2, 'unicode: Institut f\374r Festk\366rperphysik')
# 轉換坐標系
ax.text(0.95, 0.01, 'colored text in axes coords',
verticalalignment='bottom', horizontalalignment='right',
transform=ax.transAxes,
color='green', fontsize=15)
# 在2,1處畫個圓點,添加注釋
ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.axis([0, 10, 0, 10])
plt.show()

边栏推荐
- The linkedhashset set makes the elements orderly without repetition
- Why should the pointer be null after delete
- Which securities company is the largest and safest? Which securities company has good service
- Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)
- 【Redis】SortedSet类型
- 为什么要在网站上安装SSL证书?
- centos7 安装php7.2
- The left toolbar of hbuilder is missing
- 如何用项目甘特图,做好项目汇报
- CTFHub-Web-密码口令-默认口令
猜你喜欢

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

How to become a senior digital IC Design Engineer (4-3)

MySQL的下载和安装

高并发的理解与设计方案

HBuilder左侧工具栏不见了

How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code

Sysbench Pressure Test Oracle (installation and use examples)

项目研发,有哪些好用的免费脑图工具软件

如何用项目甘特图,做好项目汇报

Mipi d-phy -- contents of HS and LP agreements
随机推荐
Eliminate the hover effect when the button is disabled
Understand flex layout in an article
Koa quick start
Day10 enumeration class and annotation
What is the Valentine's Day gift given by the operator to the product?
String segment combination
安装kibana
table通过伪类实现 另类自适应
[redis] get to know redis for the first time
MySQL details - aggregation and grouping
Sysbench Pressure Test Oracle (installation and use examples)
String attribute exercise
Which securities company is the largest and safest? Which securities company has good service
PHP的exec函数
Koa 快速入門
PHP的system函数
B1006 output integer in another format
大三下期末考试
There is a time delay for the click event on the mobile terminal. What is the delay time? How to solve it?
三角函数计算