![]() To this purpose, we exploit the matplotlib function. annotate() function and all the input parameters used in the present example.Īs anticipated in the introduction, we want to improve the graphical outcome and the efficiency of our cursor by popping in a small framework, containing the coordinates of the selected point, at each mouse click. The coordinate system that xytext is given in Creating the Annotating Frameworks Annotate If you are not interested in this feature, you can skip to the next section in which we will see how to store and print the values selected by the cursor. In the next steps, we will see how to define the framework, containing the coordinates of the selected point, that will pop in at each mouse click. #defining the cursorĬursor = Cursor(ax, horizOn = True, vertOn=True, color='red', linewidth=1,Īt this point, we completed the definition of our cursor, if we were to show the plot, we would get the result displayed in Figure 1.įigure 1: Matplotlib window displaying the initial plot and the cursor (red lines). Īll the properties defined within the function Cursor, are assigned to the variable “cursor”. The last input parameter is useblit, we set it to True since it generally improves the performance of interactive figures by “not re-doing work we do not have to” (if you are interested in the process of Blitting, please visit: ).Īll the input parameters of the function Cursorare summarized in Table 1 and additional documentation can be found at. It is also possible to specify some properties of the line, like the color and the thickness (using linewidth). ![]() ![]() The function takes as input the axes in which we want to display the cursor (“ax” in this case) and other properties of the cursor itself namely horizOn and vertOn, which generate an horizontal and a vertical line that univocally identify the cursor while it is hovering on the plot their value can be set to True or False, depending on how we want to identify the cursor. To introduce a cursor in our plot, we first have to define all its properties to do that, we exploit the function Cursor, from the matplotlib.widget package. Cursor()function and all the input parameters used in the present example. Use blitting for improving the performance #PlottingĪxes defining the space in which the button will be located To this purpose, we entirely rely on the matplotlib.pyplotpackage. In the next step we define the plotting window and plot our function. All these procedures are described in the following code-lines. Of course, this is only one possible example, any function is good for the final goal of this article. The “y” array is defined by the following function:īoth the sin() and the exponential function are introduced using NumPy. linspace(), which will generate an array of 100 equally spaced numbers from 0 to 10. The “x” array is defined by exploiting the NumPy function. In order to use our cursor on a real plot, we introduce an initial function by defining two NumPy arrays, “x” and “y”. Finally, from the matplotlib.widgetpackage, we import the function Cursor, which will be used for the creation of the interactive cursor. We will use NumPyfor defining an initial function that will be then displayed using matplotlib.pyplot. # Unzipping the coord list in two different arraysĪs to begin, we import the libraries and the packages that will be used in this example. ![]() # printing the values of the selected pointįig.canvas.mpl_connect('button_press_event', onclick) # Function for storing and showing the clicked values #x and y arrays for definining an initial functionĬursor = Cursor(ax, horizOn=True, vertOn=True, useblit=True,Īnnot = ax.annotate("", xy=(0,0), xytext=(-40,40),textcoords="offset points",ībox=dict(boxstyle='round4', fc='linen',ec='k',lw=1), Every time a point on the window is clicked with the cursor, an annotating box containing the values of the point coordinates is displayed.Īnd here’s the code that we’ll discuss in this article that leads to this output: import numpy as np ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |