如何绘制几个图形并利用[matplotlib]中的导航按钮 [英] How to plot several graphs and make use of the navigation button in [matplotlib]

查看:42
本文介绍了如何绘制几个图形并利用[matplotlib]中的导航按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最新版本的 matplotlib 会自动在 graph 下创建导航按钮.但是,我在网上看到的例子都只是展示了如何创建一个图形,从而使按钮[Next][Previous]没有用.如何绘制多个图形并利用这些按钮?

例如,我想将 sin() cos() graph 0 度制作为 360 度.

现在我这样做:

  import scipy从 matplotlib 导入 pyplotDataRange =范围(0,360)DataRange =地图(scipy.deg2rad,DataRange)数据 1 = 地图(scipy.sin,数据范围)数据 2 = 地图(scipy.cos,数据范围)pyplot.plot(数据1)pyplot.show() # <--- 如果我排除这个pyplot.plot(数据2)pyplot.show()

将显示 sin() 图.当我关闭窗口时,将显示 cos 图.如果我排除第一个 pyplot.show(),两者都将显示在同一图中.

如何制作它,以便在我按下下一步"按钮时显示第二张图?

解决方案

根据

或者,您可以使用以下方法在两个数字被点击时在它们之间交替:

导入scipy从 matplotlib 导入 pyplotDataRange =范围(0,360)DataRange =地图(scipy.deg2rad,DataRange)数据 1 = 地图(scipy.sin,数据范围)数据 2 = 地图(scipy.cos,数据范围)切换=真def onclick(event):全局切换切换=不切换event.canvas.figure.clear()如果切换:event.canvas.figure.gca().plot(Data1)别的:event.canvas.figure.gca().plot(Data2)event.canvas.draw()无花果= pyplot.figure()fig.canvas.mpl_connect('button_press_event', onclick)pyplot.plot(数据1)pyplot.show()

The latest version of matplotlib automatically creates navigation buttons under the graph. However, the examples I see in the Internet all just show how to create one graph, thus making the button [Next] and [Previous] useless. How do I plot several graphs and make use of those buttons?

For example I want to make graph for sin() and cos() from 0 degree to 360 degree.

Right now I do it like this:

import scipy
from matplotlib import pyplot

DataRange = range(0, 360)
DataRange = map(scipy.deg2rad, DataRange)
Data1 = map(scipy.sin, DataRange)
Data2 = map(scipy.cos, DataRange)
pyplot.plot(Data1)
pyplot.show()   # <--- if I exclude this
pyplot.plot(Data2)
pyplot.show()

The sin() graph will be shown. It is when I close the window that the cos graph will be shown. If I exclude the first pyplot.show(), both will be shown in the same figure.

How to make it so that the second graph is shown when I press the Next button?

解决方案

According to the documentation, the Forward and Back buttons are used to allow you to go back to a previous view of a single figure. So for example if you used the Zoom-to-rectangle feature, the Back button would return you to the previous display. Depending on your backend, it is possible hook when these buttons are pressed.

You can obviously plot several graphs at the same time using subplot as follows:

import scipy
from matplotlib import pyplot

DataRange = range(0, 360)
DataRange = map(scipy.deg2rad, DataRange)
Data1 = map(scipy.sin, DataRange)
Data2 = map(scipy.cos, DataRange)

pyplot.subplot(211)
pyplot.plot(Data1)
pyplot.subplot(212)
pyplot.plot(Data2)
pyplot.show()

Giving you:

Alternatively, you could alternate between your two figures when they are clicked on using the following:

import scipy
from matplotlib import pyplot

DataRange = range(0, 360)
DataRange = map(scipy.deg2rad, DataRange)
Data1 = map(scipy.sin, DataRange)
Data2 = map(scipy.cos, DataRange)

toggle = True

def onclick(event):
    global toggle

    toggle = not toggle
    event.canvas.figure.clear()

    if toggle:
        event.canvas.figure.gca().plot(Data1)
    else:
        event.canvas.figure.gca().plot(Data2)

    event.canvas.draw()

fig = pyplot.figure()
fig.canvas.mpl_connect('button_press_event', onclick)

pyplot.plot(Data1)
pyplot.show()

这篇关于如何绘制几个图形并利用[matplotlib]中的导航按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆