当前位置: 首页>>代码示例>>Python>>正文


Python Figure.subplots方法代码示例

本文整理汇总了Python中matplotlib.figure.Figure.subplots方法的典型用法代码示例。如果您正苦于以下问题:Python Figure.subplots方法的具体用法?Python Figure.subplots怎么用?Python Figure.subplots使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在matplotlib.figure.Figure的用法示例。


在下文中一共展示了Figure.subplots方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FourierDemoFrame

# 需要导入模块: from matplotlib.figure import Figure [as 别名]
# 或者: from matplotlib.figure.Figure import subplots [as 别名]
class FourierDemoFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        panel = wx.Panel(self)

        # create the GUI elements
        self.createCanvas(panel)
        self.createSliders(panel)

        # place them in a sizer for the Layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, 1, wx.EXPAND)
        sizer.Add(self.frequencySliderGroup.sizer, 0,
                  wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
        sizer.Add(self.amplitudeSliderGroup.sizer, 0,
                  wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, border=5)
        panel.SetSizer(sizer)

    def createCanvas(self, parent):
        self.lines = []
        self.figure = Figure()
        self.canvas = FigureCanvas(parent, -1, self.figure)
        self.canvas.callbacks.connect('button_press_event', self.mouseDown)
        self.canvas.callbacks.connect('motion_notify_event', self.mouseMotion)
        self.canvas.callbacks.connect('button_release_event', self.mouseUp)
        self.state = ''
        self.mouseInfo = (None, None, None, None)
        self.f0 = Param(2., minimum=0., maximum=6.)
        self.A = Param(1., minimum=0.01, maximum=2.)
        self.createPlots()

        # Not sure I like having two params attached to the same Knob,
        # but that is what we have here... it works but feels kludgy -
        # although maybe it's not too bad since the knob changes both params
        # at the same time (both f0 and A are affected during a drag)
        self.f0.attach(self)
        self.A.attach(self)

    def createSliders(self, panel):
        self.frequencySliderGroup = SliderGroup(
            panel,
            label='Frequency f0:',
            param=self.f0)
        self.amplitudeSliderGroup = SliderGroup(panel, label=' Amplitude a:',
                                                param=self.A)

    def mouseDown(self, evt):
        if self.lines[0].contains(evt)[0]:
            self.state = 'frequency'
        elif self.lines[1].contains(evt)[0]:
            self.state = 'time'
        else:
            self.state = ''
        self.mouseInfo = (evt.xdata, evt.ydata,
                          max(self.f0.value, .1),
                          self.A.value)

    def mouseMotion(self, evt):
        if self.state == '':
            return
        x, y = evt.xdata, evt.ydata
        if x is None:  # outside the axes
            return
        x0, y0, f0Init, AInit = self.mouseInfo
        self.A.set(AInit + (AInit * (y - y0) / y0), self)
        if self.state == 'frequency':
            self.f0.set(f0Init + (f0Init * (x - x0) / x0))
        elif self.state == 'time':
            if (x - x0) / x0 != -1.:
                self.f0.set(1. / (1. / f0Init + (1. / f0Init * (x - x0) / x0)))

    def mouseUp(self, evt):
        self.state = ''

    def createPlots(self):
        # This method creates the subplots, waveforms and labels.
        # Later, when the waveforms or sliders are dragged, only the
        # waveform data will be updated (not here, but below in setKnob).
        self.subplot1, self.subplot2 = self.figure.subplots(2)
        x1, y1, x2, y2 = self.compute(self.f0.value, self.A.value)
        color = (1., 0., 0.)
        self.lines += self.subplot1.plot(x1, y1, color=color, linewidth=2)
        self.lines += self.subplot2.plot(x2, y2, color=color, linewidth=2)
        # Set some plot attributes
        self.subplot1.set_title(
            "Click and drag waveforms to change frequency and amplitude",
            fontsize=12)
        self.subplot1.set_ylabel("Frequency Domain Waveform X(f)", fontsize=8)
        self.subplot1.set_xlabel("frequency f", fontsize=8)
        self.subplot2.set_ylabel("Time Domain Waveform x(t)", fontsize=8)
        self.subplot2.set_xlabel("time t", fontsize=8)
        self.subplot1.set_xlim([-6, 6])
        self.subplot1.set_ylim([0, 1])
        self.subplot2.set_xlim([-2, 2])
        self.subplot2.set_ylim([-2, 2])
        self.subplot1.text(0.05, .95,
                           r'$X(f) = \mathcal{F}\{x(t)\}$',
                           verticalalignment='top',
                           transform=self.subplot1.transAxes)
        self.subplot2.text(0.05, .95,
#.........这里部分代码省略.........
开发者ID:Carreau,项目名称:matplotlib,代码行数:103,代码来源:fourier_demo_wx_sgskip.py

示例2: VelociraptorGui

# 需要导入模块: from matplotlib.figure import Figure [as 别名]
# 或者: from matplotlib.figure.Figure import subplots [as 别名]

#.........这里部分代码省略.........
        self.rides = bike.read_db_file(year='all')
        self.years = sorted(list(set(ride['timestamp'].year for ride in
            self.rides)), reverse=True)
        self.year_combo['values'] = self.years

    def update_rides_view(self):
        self.viewable_rides = [ride for ride in self.rides if
                ride['timestamp'].year == int(self.year.get())]
        self.rides_view.delete(*self.rides_view.get_children())
        for ride in self.viewable_rides:
            self.rides_view.insert('', 'end', values=format_ride(ride))

    def get_graph_data(self):
        cumsum = list(itertools.accumulate(ride['distance'] for ride in
                        self.viewable_rides))
        dates = [ride['timestamp'] for ride in self.viewable_rides]
        speeds = [ride['distance'] / ride['duration'] for ride in
                self.viewable_rides]
        return cumsum, dates, speeds

    def update_graph_view(self):
        cumsum, dates, speeds = self.get_graph_data()
        self.ax1.clear()
        self.ax2.clear()
        self.ax1.plot(dates, cumsum)
        self.ax2.plot(dates, speeds)
        self.ax1.set_ylabel('distance (km)')
        self.ax2.set_ylabel('vitesse (km/h)')
        self.fig.autofmt_xdate()
        self.graph_view.draw()
        
    def _init_graph_view(self):
        matplotlib.style.use('ggplot')
        self.fig = Figure(figsize=(4, 4), tight_layout=True)
        cumsum, dates, speeds = self.get_graph_data()
        self.ax1, self.ax2 = self.fig.subplots(2, sharex=True)
        self.ax1.plot(dates, cumsum)
        self.ax2.plot(dates, speeds)
        self.ax1.set_ylabel('distance (km)')
        self.ax2.set_ylabel('vitesse (km/h)')
        self.fig.autofmt_xdate()
        self.graph_view = FigureCanvasTkAgg(self.fig, master=self)
        self.graph_view.draw()

        self.graph_view.get_tk_widget().grid(column=1, row=1, sticky='nsew')

    def update_stats(self):
        stats = bike.get_stats(self.viewable_rides)
        stats_text = 'Distance totale : {:.1f} km\n'.format(stats['tot_distance'])
        stats_text += 'Durée totale : {:.1f} h\n'.format(stats['tot_duration'])
        stats_text += 'Distance moyenne : {:.1f} km\n'.format(stats['mean_distance'])
        stats_text += 'Durée moyenne : {:.1f} h\n'.format(stats['mean_duration'])
        stats_text += 'Vitesse moyenne : {:.2f} km/h\n'.format(stats['speed'])
        self.stats_text.set(stats_text)

    def _init_stats_view(self):
        self.stats_text = tk.StringVar()
        self.stats_view = tk.Label(self, textvariable=self.stats_text,
                justify=tk.LEFT, anchor=tk.NW)
        self.stats_view.grid(column=1, row=2)
        self.update_stats()

    def add_ride(self):
        dialog = RideDetailDialog(self, 'Ajouter une randonnée')
        result = dialog.result
        dialog.destroy()
        if result:
            self.load_data()
            self.update_rides_view()
            self.update_graph_view()
            self.update_stats()

    def edit_ride(self, event=None):
        try:
            iid = self.rides_view.selection()[0]
        except IndexError:
            return
        rideid = int(self.rides_view.item(iid, 'values')[0])
        ride = self.rides[rideid]
        dialog = RideDetailDialog(self, 'Modifier une randonnée', ride=ride)
        result = dialog.result
        dialog.destroy()
        if result:
            self.load_data()
            self.update_rides_view()
            self.update_graph_view()
            self.update_stats()

    def del_ride(self):
        try:
            iid = self.rides_view.selection()[0]
        except IndexError:
            return
        rideid = int(self.rides_view.item(iid, 'values')[0])
        self.rides.pop(rideid)
        bike.update_db(self.rides)
        self.load_data()
        self.update_rides_view()
        self.update_graph_view()
        self.update_stats()
开发者ID:loicseguin,项目名称:bike,代码行数:104,代码来源:Velociraptor.py


注:本文中的matplotlib.figure.Figure.subplots方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。