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

时间:2016-03-15 14:38:37

标签: python matplotlib graph

我真的想在这里做一些简单的事情,但经常苦苦挣扎。

我试图让用户将点输入到将实时更新的图表上。

这是情景。

我的图表将在展览中投影,然后当用户输入他们上班需要多长时间时,它会在图表上显示该点。

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

以下是我最终做的非常有效的事情!

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()

1 个答案:

答案 0 :(得分:0)

我把一些东西放在一起。它仍然需要一些工作,但这应该给你一个很好的起点

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()

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