Python 通过用户输入在图形上绘制点

Python 通过用户输入在图形上绘制点,python,matplotlib,graph,Python,Matplotlib,Graph,我真的很想在这里做一些简单的事情,但是很挣扎 我试图让用户在一个图形上输入点,该图形将实时更新 下面是一个场景 我的图表将在展览中投影,然后当用户输入他们工作所需的时间时,它会在图表上显示该点 随着越来越多的用户输入他们的数据,我最终会生成一个图表,显示彼此之间的所有输入 这是我最后做的,效果非常好 from pylab import * def click(event): """If the left mouse button is pressed: draw a little squ

我真的很想在这里做一些简单的事情,但是很挣扎

我试图让用户在一个图形上输入点,该图形将实时更新

下面是一个场景

我的图表将在展览中投影,然后当用户输入他们工作所需的时间时,它会在图表上显示该点

随着越来越多的用户输入他们的数据,我最终会生成一个图表,显示彼此之间的所有输入

这是我最后做的,效果非常好

from pylab import *

def click(event):
   """If the left mouse button is pressed: draw a little square. """
   tb = get_current_fig_manager().toolbar
   if event.button==1 and event.inaxes and tb.mode == '':
       x,y = event.xdata,event.ydata
       plot([x],[y],'rs')
       draw()

plt.title('How long does it take you to get to work?\nClick the spot.')
plt.xlabel('Kms')
plt.ylabel('Hours')
plt.legend()

plot((arange(100)/18))
gca().set_autoscale_on(False)
connect('button_press_event',click)
annotate('Line Of Disadvantage', xy=(20, 1), xytext=(7, 3),
            arrowprops=dict(facecolor='black', shrink=0.05))
annotate('Most Disadvantaged',xy=(20, 1), xytext=(5, 5)),
annotate('Least Disadvantaged',xy=(20, 1), xytext=(70, 1)),

show()

我整理了一点东西。这仍然需要一些工作,但这应该给你一个良好的起点

import matplotlib.pyplot as plt


def replot(distance_list, duration_list):
    # this function will produce the desired output chart (also used to reload the chart after data has been added)

    # close all older plots
    plt.close('all')

    plt.plot(distance_list, duration_list, 'ro')

    # refer to matplotlib documentation to learn how to format your chart.
    # I suggest, you add a chart title, axis legends etc.
    # you can also change the size and color of dots

    plt.xlabel('x-axis')
    plt.ylabel('y-axis')
    plt.title('Title')

    # maximise the plot window and show it:
    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()
    plt.tight_layout()
    plt.show()


def user_interaction():
    # this function defines/loads the information to be displayed
    # it asks for user input 
    # it stores the new user input and triggers the chart to be refreshed

    # define lists with information
    distance_list = [1,2,3,4]

    duration_list = [1,4,9,16]

    # I suggest to manipulate the script in the followig way:
    # you want to preserve the infomration that users input. Instead of defining the lists above, you want to load the "previous" information:
    # there are many ways of storing and loading information. You could use .csv, .txt files or you could save a dict in json format....

    # ask for user input
    new_distance = float(raw_input("How far away is your work, in km? "))
    new_duration = float(raw_input("How long does it take you to get there, in min? "))

    # append user input to existing list
    distance_list.append(new_distance)
    duration_list.append(new_duration)

    # save the appended lists
    # I suggest you write the user information to file. That way, they can be re-loaded when the next user enters data. The data will also survive computer shut-dwon etc.


    # now, send the two lists to a plotting function
    replot( distance_list, duration_list )


if __name__=='__main__':

    user_interaction()

您需要找到一种“再次启动脚本”的方法。当下一个用户出现时,脚本需要再次向他询问相同的问题。也许其他人能帮上忙

我整理了一点东西。这仍然需要一些工作,但这应该给你一个良好的起点

import matplotlib.pyplot as plt


def replot(distance_list, duration_list):
    # this function will produce the desired output chart (also used to reload the chart after data has been added)

    # close all older plots
    plt.close('all')

    plt.plot(distance_list, duration_list, 'ro')

    # refer to matplotlib documentation to learn how to format your chart.
    # I suggest, you add a chart title, axis legends etc.
    # you can also change the size and color of dots

    plt.xlabel('x-axis')
    plt.ylabel('y-axis')
    plt.title('Title')

    # maximise the plot window and show it:
    figManager = plt.get_current_fig_manager()
    figManager.window.showMaximized()
    plt.tight_layout()
    plt.show()


def user_interaction():
    # this function defines/loads the information to be displayed
    # it asks for user input 
    # it stores the new user input and triggers the chart to be refreshed

    # define lists with information
    distance_list = [1,2,3,4]

    duration_list = [1,4,9,16]

    # I suggest to manipulate the script in the followig way:
    # you want to preserve the infomration that users input. Instead of defining the lists above, you want to load the "previous" information:
    # there are many ways of storing and loading information. You could use .csv, .txt files or you could save a dict in json format....

    # ask for user input
    new_distance = float(raw_input("How far away is your work, in km? "))
    new_duration = float(raw_input("How long does it take you to get there, in min? "))

    # append user input to existing list
    distance_list.append(new_distance)
    duration_list.append(new_duration)

    # save the appended lists
    # I suggest you write the user information to file. That way, they can be re-loaded when the next user enters data. The data will also survive computer shut-dwon etc.


    # now, send the two lists to a plotting function
    replot( distance_list, duration_list )


if __name__=='__main__':

    user_interaction()


您需要找到一种“再次启动脚本”的方法。当下一个用户出现时,脚本需要再次向他询问相同的问题。也许其他人能帮上忙

直方图或条形图不是表示此类数据的更好方法吗?我开始使用条形图,而不是在y轴上绘制随机数来展开它们。请帮帮我。看看上面更新的代码。你是否让python代码本身让用户输入一个值,然后更新条形图?还有,你的文本文件就是用来保存输入的吗?Hi@DavidG-最后的解决方案见上文。这是表示你将获得的数据的好方法。我甚至没有想过这样做。直方图或条形图不是更好的表示此类数据的方法吗?我开始使用条形图,而不是在y轴上绘制随机数来展开它们。请帮帮我。看看上面更新的代码。你是否让python代码本身让用户输入一个值,然后更新条形图?还有,你的文本文件就是用来保存输入的吗?Hi@DavidG-最后的解决方案见上文。这是表示你将获得的数据的好方法。我甚至没有想过这样做。我得到了以下错误:“tkapp”对象没有属性“showMaximized”,而且,当我运行模块时,没有弹出的图形。在Rasbpian上使用Python3.4。好的,这就是我现在的位置。有些工作@rde有关更新的代码和问题,请参阅原始帖子。@LukeEventer,您能指出您到底遇到了什么问题吗?由于您使用的是Python3,而我使用的是2.7,因此某些命令可能无法工作(例如figManager/fullscreen)。请参见上文,了解我最终所做的工作,这些工作对我来说非常有效@rdeI收到以下错误:“tkapp”对象没有属性“showMaximized”。此外,当我运行模块时,没有弹出的图形。在Rasbpian上使用Python3.4。好的,这就是我现在的位置。有些工作@rde有关更新的代码和问题,请参阅原始帖子。@LukeEventer,您能指出您到底遇到了什么问题吗?由于您使用的是Python3,而我使用的是2.7,因此某些命令可能无法工作(例如figManager/fullscreen)。请参见上文,了解我最终所做的工作,这些工作对我来说非常有效@rde