python - 如何为两个函数交集的区域编写标题?
问题描述
假设c
在[0,0.5]
和r=sqrt(c)
。
我正在使用以下代码填充 和 之间的区域0
,r
并且我想在图中的该区域内写标签:
fig, ax1 = plt.subplots(1, 1, sharex=True);
ax1.fill_between(c, 0, r, label='region 1 ')
怎么可能做到这一点?
另外,当我设置时c=constant
,它会产生一条线,我愿意将它显示为一条带有图例的实线。我怎样才能做到这一点?
新年快乐!
解决方案
ax.legend()
可以为每个带有label=...
. 可以设置以“轴坐标”测量bbox_to_anchor=(x,y)
的锚点(从左侧绘图边界到右侧的 1,从底部的 0 到顶部的 1)。告诉图例边界框的哪个点将定位在锚点处。x,y
loc=
ax.axhline()
绘制一条恒定的水平线。
import matplotlib.pyplot as plt
import numpy as np
c = np.linspace(0, 0.5)
r = np.sqrt(c)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 4))
ax1.fill_between(c, 0, r, label='region 1')
ax1.legend(loc='lower right', bbox_to_anchor=(0.8, 0.2), facecolor='ivory', framealpha=0.9)
ax1.margins(x=0, y=0)
constant = 0.5
ax2.axhline(np.sqrt(constant), label=f'sqrt of {constant}', lw=2)
ax2.legend()
ax2.set_xlim(0, 0.5)
ax2.set_ylim(0, 1)
plt.show()
推荐阅读
- ruby-on-rails - Rails如何在参数中封装2个var?
- java - 使用非静态内部类在 MainActivity 中接收广播
- python-3.x - 在 Linux 命令中使用命令“import os”失败?
- c# - 广义 DocumentDB / CosmosDB 分区策略
- javascript - jquery加载多行
- qt - Seg Fault 创建新的 QTcpSocket
- java - 无法解析符号“MODE_PRIVATE”
- java - 如何确定哪个 Xlint 选项对应于编译器警告?
- python - 为什么python的'不是'不一致?
- c# - 创建使用一个 foreach 而不是多个的方法