如何使用python在graph中添加标记,如下所示?

2024-04-24 13:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想设计一个像下面这样的图,但是,我不知道如何添加像图中所示那样的标记。 有人能帮我吗?你知道吗

为了绘制图形,我使用了python中的matplotlib包。你知道吗

enter image description here


Tags: 标记图形matplotlib绘制
2条回答

您可以使用Bokeh框架作为matplotlib的替代方案。(我个人觉得好多了。)

for plot in [p1, p3, p5]:
        plot.add_tools(HoverTool(
            tooltips=[
                ("(x,y, name)", "($x, $y, $name"),  # use @{ } for field names with spaces
            ],
        formatters={
            'date': 'datetime',  # use 'datetime' formatter for 'date' field
            'adj close': 'printf',  # use 'printf' formatter for 'adj close' field
            # use default 'numeral' formatter for other fields
        },

        # display a tooltip whenever the cursor is vertically in line with a glyph
        # mode='hline'
        mode='mouse'
    ))

enter image description here

在matplotlib docAnnotations中,可以使用以下代码修改示例

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.plot(2, 1, marker = "v", color='blue', fillstyle='none')
bbox_props = dict(boxstyle="square,pad=0.3", fc="white", ec="black", lw=1.2)
t = ax.annotate('local max\n x = 2, y = 1', xy=(2, 1), xytext=(3, 1.5),
        arrowprops=dict(arrowstyle="-", facecolor='black'), bbox=bbox_props,
        )

ax.set_ylim(-2, 2)
plt.show()

enter image description here

相关问题 更多 >