python中matplotlib和mpld3创建plot时的重叠图例

2024-05-12 12:46:45 发布

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

我是python编程的初学者,我很难弄清楚如何解决我的问题。在

问题是,当我在python中使用matplotlib和mpld3创建一个plot时,这个plot就像我所期望的那样,但是这个plot的图例确实是错误的,因为图例相互重叠。仅当其他数据的图例与最新数据重叠时,才会显示最新数据的绘图图例。在

下面是我的情节图:

Image

以下是在循环中创建绘图和图例的代码:

plt.rcParams.update({'font.size': 13})
fig, ax = plt.subplots()
for i in range(3,5):
        rd1 = pd.read_sql_query("SELECT press, rs FROM basic_chart WHERE 
        cs = "+str(i), app.config['SQLALCHEMY_DATABASE_URI'])
        print(rd1)
        pt = ax.plot(rd1['press'],rd1['rs'],'-o')
        plugins.connect(fig, plugins.InteractiveLegendPlugin([pt],[str(i)]))
html = mpld3.fig_to_html(fig)

我认为主要的问题在于交互式图例代码,但我没有找到正确的方法来纠正它。我真的希望专家能帮我解决这个问题。在


Tags: 数据代码pt绘图plotfigpluginsplt
1条回答
网友
1楼 · 发布于 2024-05-12 12:46:45
plt.rcParams.update({'font.size': 13})
fig, ax = plt.subplots()
for i in range(3,5):
    rd1 = pd.read_sql_query("SELECT press, rs FROM basic_chart WHERE 
    cs = "+str(i), app.config['SQLALCHEMY_DATABASE_URI'])
    print(rd1)
    pt = ax.plot(rd1['press'],rd1['rs'],'-o')

axhandles, axlabels = ax.get_legend_handles_labels()
plugins.connect(fig, plugins.InteractiveLegendPlugin(axhandles, axlabels))
html = mpld3.fig_to_html(fig)

通过拥有插件.connect在循环中,您将创建两个单独的图例。 绘图之后,从绘图中获取句柄和标签,并在调用InteractiveLegendPlugin时使用它。在

相关问题 更多 >