将文本放在matplotlib图的左上角

举报
墨理学AI 发表于 2022/01/10 22:57:27 2022/01/10
【摘要】 官网链接 将文本放在matplotlib图的左上角 import numpy as np import matplotlib.pyplot as plt import matplotlib.colle...

官网链接

将文本放在matplotlib图的左上角

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as collections
t = np.arange(0.0, 2, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = 1.2*np.sin(4*np.pi*t)

fig = plt.figure(figsize=(15, 6))
ax = fig.add_subplot(1, 2, 1)

ax.set_title('using span_where')
# 设置曲线
ax.plot(t, s1, color='black')
# 设置横轴
ax.axhline(0, color='black', lw=2)
# 设置文本,目标位置 左上角,距离 Y0.01 倍距离,距离 X0.95倍距离
ax.text(0.01, 0.95, "I am here ", transform=ax.transAxes, fontdict={'size': '16', 'color': 'b'})

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.8)
ax.add_collection(collection)

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5)
ax.add_collection(collection)

"""
ax2 
"""
ax2 = fig.add_subplot(1, 2, 2)

ax2.set_title('using span_where')
ax2.plot(t, s1, color='black')
ax2.axhline(0, color='black', lw=2)
# 设置文本,目标位置 左上角,距离 Y1.91 倍距离(这里说明它是以最左边为轴,需要自行调节),距离 X0.95倍距离
ax2.text(1.91, 0.95, "I am here too", transform=ax.transAxes, fontdict={'size': '16', 'color': 'b'})

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.8)
ax2.add_collection(collection)

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5)
ax2.add_collection(collection)
plt.show()
  
 

效果如下:

在这里插入图片描述

文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。

原文链接:positive.blog.csdn.net/article/details/105096493

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。