matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)-------(含绘图实例演示)

本文围绕Python绘图展开,深入解析了title、annotate和text的使用方法。介绍了title设置图像标题的常用参数及示例,包括字体大小、粗细、颜色等;阐述了annotate标注文字的语法说明;还说明了text设置文字说明的语法及案例,涵盖坐标、字体、对齐方式等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 全栈工程师核心面试 300 问深入解析(2020 版)----全文预览
Python 全栈工程师核心面试 300 问深入解析(2020 版)----欢迎订阅

1.title设置图像标题

(1)title常用参数

fontsize设置字体大小,默认12,可选参数 [‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’,‘x-large’, ‘xx-large’]
fontweight设置字体粗细,可选参数 [‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’]
fontstyle设置字体类型,可选参数[ ‘normal’ | ‘italic’ | ‘oblique’ ],italic斜体,oblique倾斜
verticalalignment设置水平对齐方式 ,可选参数 : ‘center’ , ‘top’ , ‘bottom’ ,‘baseline’
horizontalalignment设置垂直对齐方式,可选参数:left,right,center
rotation(旋转角度)可选参数为:vertical,horizontal 也可以为数字
alpha透明度,参数值0至1之间
backgroundcolor标题背景颜色
bbox给标题增加外框 ,常用参数如下:
boxstyle方框外形
facecolor(简写fc)背景颜色
edgecolor(简写ec)边框线条颜色
edgewidth边框线条大小

(2)title例子:

plt.title(‘Interesting Graph’,fontsize=‘large’,fontweight=‘bold’) 设置字体大小与格式
plt.title(‘Interesting Graph’,color=‘blue’) 设置字体颜色
plt.title(‘Interesting Graph’,loc =‘left’) 设置字体位置
plt.title(‘Interesting Graph’,verticalalignment=‘bottom’) 设置垂直对齐方式
plt.title(‘Interesting Graph’,rotation=45) 设置字体旋转角度
plt.title(‘Interesting’,bbox=dict(facecolor=‘g’, edgecolor=‘blue’, alpha=0.65 )) 标题边框

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [3, 6, 7, 9, 2]

fig, ax = plt.subplots(1, 1)
ax.plot(x, y, label='trend')
ax.set_title('title test', fontsize=12, color='r')

plt.show()

在这里插入图片描述

2.annotate标注文字

(1)annotate语法说明 :annotate(s=‘str’ ,xy=(x,y) ,xytext=(l1,l2) ,…)

s 为注释文本内容
xy 为被注释的坐标点
xytext 为注释文字的坐标位置
xycoords 参数如下:

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 t
extcoords 设置注释文字偏移量

     | 参数 | 坐标系 | 
     | 'figure points' | 距离图形左下角的点数量 | 
     | 'figure pixels' | 距离图形左下角的像素数量 | 
     | 'figure fraction' | 0,0 是图形左下角,1,1 是右上角 | 
     | 'axes points' | 距离轴域左下角的点数量 | 
     | 'axes pixels' | 距离轴域左下角的像素数量 | 
     | 'axes fraction' | 0,0 是轴域左下角,1,1 是右上角 | 
     | 'data' | 使用轴域数据坐标系 |

arrowprops #箭头参数,参数类型为字典dict

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 总长度为分数“缩水”从两端
facecolor 箭头颜色

bbox给标题增加外框 ,常用参数如下:
boxstyle方框外形
facecolor(简写fc)背景颜色
edgecolor(简写ec)边框线条颜色
edgewidth边框线条大小
bbox=dict(boxstyle=‘round,pad=0.5’, fc=‘yellow’, ec=‘k’,lw=1 ,alpha=0.5) #fc为facecolor,ec为edgecolor,lw为lineweight

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 6)
y = x * x

plt.plot(x, y, marker='o')
for xy in zip(x, y):
    plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-20, 10), textcoords='offset points')

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

# 显示中文和显示负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 生成数据
# 生成30个-5到2等分元素
x = np.linspace(-5, 2, 30)
y1 = x**3 + 5*x**2 + 10
# y2是y1的导数
y2 = 3*x**2 + 10*x
# y3是y2的导数
y3 = 6*x + 10
# y4是一条曲线
y4 = x**2

# 基本绘图方法
# plt.plot(x, y1)
# plt.show()

# 高级绘图
# 对象绘图,返回一个画布,一个绘图对象,共享X轴
fig, ax = plt.subplots(2, 2, figsize=(10, 8), sharex=True)


# 注意,上面ax是plt返回的绘图对象,本来绘图设置都是plt.,后面都是使用
# 指定绘图的位置,进行绘图
ax[0, 0].plot(x, y1, 'b--')
# 添加注释,传入注释内容,注释的箭头的点坐标,注释文字的坐标位置,
# shrink设置箭头末端和文字起点的间距,箭头自己会根据箭头起点和文字起点自适应大小和倾斜角度
ax[0, 0].annotate('我是注释', xy=(-3, 30), xytext=(-1, 35), arrowprops=dict(facecolor='red', edgecolor='red', shrink=0.05))

# 每个图都可以单独设置,指定ax对象进行设置
# 绘制另外3个图

ax[0, 1].plot(x, y2, 'r--')

ax[1, 0].plot(x, y3, 'b*')
ax[1, 0].set_xlabel('x轴')

ax[1, 1].plot(x, y4, 'r*')
ax[1, 1].set_xlabel('x轴')

# 展示所有图形
plt.show()

在这里插入图片描述

3.text设置文字说明

(1)text语法说明

text(x,y,string,fontsize=15,verticalalignment=“top”,horizontalalignment=“right”)

x,y:表示坐标值上的值
string:表示说明文字
fontsize:表示字体大小
verticalalignment:垂直对齐方式 ,参数:[ ‘center’ | ‘top’ | ‘bottom’ | ‘baseline’ ]
horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ]
xycoords选择指定的坐标轴系统:

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 t

arrowprops #箭头参数,参数类型为字典dict

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 总长度为分数“缩水”从两端
facecolor 箭头颜色
bbox给标题增加外框 ,常用参数如下:

boxstyle方框外形
facecolor(简写fc)背景颜色
edgecolor(简写ec)边框线条颜色
edgewidth边框线条大小
bbox=dict(boxstyle=‘round,pad=0.5’, fc=‘yellow’, ec=‘k’,lw=1 ,alpha=0.5) #fc为facecolor,ec为edgecolor,lw为lineweight

(2)案例

文字格式与位置:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = "This is a really long string"

plt.text(4, 1, t, ha='left', rotation=15, wrap=True)

plt.text(6, 5, t, ha='left', rotation=15, wrap=True)

plt.text(5, 5, t, ha='right', rotation=-15, wrap=True)

plt.text(5, 10, t, fontsize=18, style='oblique', ha='center', va='top', wrap=True)

plt.text(3, 4, t, family='serif', style='italic', ha='right', wrap=True)

plt.text(-1, 0, t, ha='left', rotation=-15, wrap=True)

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt

# 显示中文和显示负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

nrows, ncols = 3, 3
fig, ax = plt.subplots(nrows, ncols)

for m in range(nrows):
    for n in range(ncols):
        # 隐藏XY轴刻度
        ax[m, n].set_xticks([])
        ax[m, n].set_yticks([])
        # 添加文字
        ax[m, n].text(0.25, 0.5, "axes[%d, %d]" % (m, n), fontsize=15, color='red')

ax[1, 1].text(0.5, 0.2, 'subplots演示', fontsize=15, color='blue', horizontalalignment='center')

# plt直接设置,是默认在最后一个子绘图对象上面设置
plt.text(0, 0, 'figure画布注释', ha='left', rotation=-15, wrap=True)

plt.show()

在这里插入图片描述

<think>我们被要求绘制一个直角坐标系图,其中包(x=-60,y=90)。由于这是一个简单的绘图任务,通常我们会使用绘图软件或编程库来实现。考虑到用户可能需要具体的实现方法,这里将介绍两种常见的方法:使用Python的matplotlib库和手动绘制方法。方法一:使用Python和matplotlib绘制这是一种编程方法,适合需要精确绘图或自动化绘图的场景。步骤:1.导入必要的库:matplotlib.pyplot。2.创建一个图形和一个坐标轴。3.设置坐标轴的范围,确保点(-60,90)在图中可见。4.绘制坐标轴,并设置原点(0,0)。5.在点(-60,90)处绘制一个点,并可以添加标签。6.显示图形。示例代码:```pythonimportmatplotlib.pyplotas plt#创建图形和坐标轴fig, ax= plt.subplots() #设置坐标轴范围,x轴从-70到10,y轴从0到100(根据点(-60,90)调整) ax.set_xlim([-70,10])ax.set_ylim([0,100]) #绘制坐标轴ax.axhline(y=0, color='k',linewidth=0.5)#水平轴ax.axvline(x=0, color='k', linewidth=0.5)#垂直轴#绘制点(-60,90)ax.plot(-60,90,'ro')#红色圆点ax.annotate('(-60,90)',xy=(-60,90), xytext=(-58,92))#添加标签#设置网格ax.grid(True, linestyle='--',alpha=0.7)#显示图形plt.show()```方法二:手动绘制如果没有编程环境,可以使用纸笔或绘图软件(如GeoGebra、MicrosoftPaint等)手动绘制。步骤:1.绘制两条互相垂直的直线作为坐标轴(水平x轴,垂直y轴),在交点处标记原点(0,0)。2.在x轴上标记刻度,负方向标记-60,正方向标记适当刻度(如10,20等);在y轴上标记刻度,从0到100,确保90在图中。3.在x=-60处画一条垂直于x轴的虚线,在y=90处画一条垂直于y轴的虚线,两条虚线的交点即为(-60,90)。4.在该点处画一个明显的点,并标记坐标(-60,90)。注意:为了准确表示点(-60,90),需要确保x轴负方向有足够的空间,y轴正方向也要足够高。另外,也可以使用在线绘图工具,如Desmos、GeoGebra等,直接输入坐标点即可。考虑到用户可能没有指定工具,这里提供两种通用方法。相关问题:基于用户的问题,他们可能对坐标系绘图、标记特定点等有进一步的问题。注意:由于我们无法实际运行绘图并展示,所以提供代码和步骤供用户自行操作。</think>### 绘制直角坐标系中特定坐标点的方法 要绘制点(x=-60, y=90)的直角坐标系图形,可通过以下两种方式实现: --- #### **方法1:使用Python Matplotlib库** ```python import matplotlib.pyplot as plt # 创建坐标系 plt.figure(figsize=(8, 6)) plt.axhline(y=0, color='k', linewidth=0.8) # x轴 plt.axvline(x=0, color='k', linewidth=0.8) # y轴 # 标记坐标点 plt.plot(-60, 90, 'ro', markersize=8) # 红色点 plt.text(-62, 92, '(-60,90)', fontsize=12) # 坐标标注 # 设置坐标范围 plt.xlim(-80, 20) plt.ylim(-10, 100) # 添加网格和标签 plt.grid(linestyle='--', alpha=0.7) plt.title('直角坐标系:点(-60,90)', fontsize=14) plt.xlabel('X轴', fontsize=12) plt.ylabel('Y轴', fontsize=12) plt.show() ``` **关键参数说明**: - `xlim(-80, 20)`:确保负值X轴区域可见 - `markersize=8`:控制点的大小 - `linestyle='--'`:虚线网格增强可读性 --- #### **方法2:手动绘图步骤** 1. **建立坐标系框架** - 绘制垂直交叉的直线作为X/Y轴 - 原点(0,0)位于图纸中心 - 标注刻度(X轴每格=20单位,Y轴每格=20单位) 2. **定位目标点** - 从原点向左移动3格($ -60 = 3 \times -20 $) - 从原点向上移动4.5格($ 90 = 4.5 \times 20 $) - 公式验证: $$ \text{X位置} = \frac{-60}{\text{刻度值}} \quad \text{Y位置} = \frac{90}{\text{刻度值}} $$ 3. **标记标注** - 用红色实心圆点标记位置 - 在点上方标注`(-60,90)`文本 - 添加坐标轴标签和标题 --- **工具推荐**: - 编程工具:Python + Matplotlib(精确控制) - 在线工具:Desmos(https://www.desmos.com/calculator) - 数学软件:GeoGebra(动态交互绘图) > **注意事项**: > - 负值区域需预留足够绘图空间 > - 非对称刻度时需调整比例尺 > - 纸质绘图建议使用坐标纸
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值