简体   繁体   English

Matplotlib 制作多个图形并使用箭头进行更改 - Python

[英]Matplotlib make several graphics and use the arrow to change - Python

I have a text and I want to make a graphic of the letter-frequency every n sentences.我有一个文本,我想每n句子制作一个字母频率的图形。 I have this code to make one graphic:我有这个代码来制作一个图形:

def graphic(dic):
    x = list(range(len(dic)))
    liste = []
    valeur = []
    for i in dic:
        liste += [(dic[i],i)]
        valeur += [dic[i]]
    liste.sort()
    liste.reverse()
    valeur.sort()
    valeur.reverse()
    my_xticks = []
    for i in liste:
        my_xticks += i[1]
    xticks(x, my_xticks)
    plot(x,valeur); show()
    return liste,valeur

It returns me this:它返回给我:

在此处输入图片说明

My point is, I want to use the arrows on the top of the window to change to one graphic to an other.我的观点是,我想使用窗口顶部的箭头将一个图形更改为另一个图形。 Is this possible?这可能吗?

For example, I have a text with 10 sentences, and I want to make a graphic every 1 sentence.例如,我有一个包含 10 个句子的文本,我想每 1 个句子制作一个图形。 So, I'll have 10 graphics and I want to be able to navigate with the arrows but when I just call the function twice, it draws me 2 graph on the same page.因此,我将有 10 个图形,并且我希望能够使用箭头进行导航,但是当我只调用该函数两次时,它会在同一页面上为我绘制 2 个图形。

You can access the buttons and change their callbacks:您可以访问按钮并更改其回调:

import matplotlib.pyplot as plt

def callback_left_button(event):
    ''' this function gets called if we hit the left button'''
    print('Left button pressed')


def callback_right_button(event):
    ''' this function gets called if we hit the left button'''
    print('Right button pressed')

fig = plt.figure()

toolbar_elements = fig.canvas.toolbar.children()
left_button = toolbar_elements[6]
right_button = toolbar_elements[8]

left_button.clicked.connect(callback_left_button)
right_button.clicked.connect(callback_right_button)

Here's a way to do this without referring to a specific backend (ie it should be portable).这是一种无需引用特定后端即可执行此操作的方法(即它应该是可移植的)。 The idea is that matplotlib defines a somewhat vague interface for backends to implement.这个想法是 matplotlib 为后端实现定义了一个有点模糊的接口。 This interface is the class NavigationToolbar2 below ( github source ; possible linux source directory: /usr/lib/python3/dist-packages/matplotlib/backend_bases.py).这个接口是下面的NavigationToolbar2类( github 源代码;可能的 linux 源代码目录:/usr/lib/python3/dist-packages/matplotlib/backend_bases.py)。 This interface uses a _nav_stack object of type Stack from cbook .此接口使用来自cbook Stack类型的_nav_stack对象。 This stack keeps information about different panning information, and when something changes the toolbar calls a function _update_view and redraws the canvas.该堆栈保留有关不同平移信息的信息,并且当发生变化时,工具栏会调用函数_update_view并重新_update_view画布。 By creating our own Stack , supplying (a proxy to) it to the toolbar, and overwriting _update_view , we can make the toolbar do what we'd like.通过创建我们自己的Stack ,将它提供(代理)到工具栏,并覆盖_update_view ,我们可以让工具栏做我们想做的事情。

import matplotlib.backend_bases
import matplotlib.pyplot as plt
from numpy.random import random

# this is the data structure is used by NavigationToolbar2
# to switch between different pans.  We'll make the figure's 
# toolbar hold a proxy to such an object

from matplotlib.cbook import Stack

class StackProxy:
  def __init__(self,stack):
    self._stack = stack

  def __call__(self):
    return self._stack.__call__()

  def __len__(self):
    return self._stack.__len__()

  def __getitem__(self,ind):
    return self._stack.__getitem__(ind)

  def nop(self): pass

  # prevent modifying the stack
  def __getattribute__(self,name):
    if name == '_stack':
      return object.__getattribute__(self,'_stack')
    if name in ['push','clear','remove']:
      return object.__getattribute__(self,'nop')
    else:
      return object.__getattribute__(self._stack,name)

stack = Stack()

for data in [[random(10),random(10)] for _ in range(5)]:
  stack.push(data)

def redraw(*args):
  plt.clf()
  plt.scatter(*stack())   # stack() returns the currently pointed to item
  plt.gcf().canvas.draw()

fig = plt.gcf()
toolbar = fig.canvas.toolbar
toolbar._update_view = redraw.__get__(toolbar)
stackProxy = StackProxy(stack)
toolbar._nav_stack = stackProxy

redraw()
plt.show()

Previously, I was modifying some base classes of the buttons, but since then I've learned about some object oriented techniques of python and found this to be a much better solution.以前,我正在修改按钮的一些基类,但从那时起我了解了一些 Python 的面向对象技术,并发现这是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM