plt.close('all') fig, ax = plt.subplots(1, figsize=(12,6)) # plot and labels sc = ax.scatter(x,y) plt.xlabel(x_name) plt.ylabel(y_name) # cursor grid lines lnx = plt.plot(,, color='black', linewidth=0.3) lny = plt.plot(,, color='black', linewidth=0.3) lnx.set_linestyle('None') lny.set_linestyle('None') # annotation annot = ax.annotate("", xy=(0,0), xytext=(5,5),textcoords="offset points") t_visible(False) # xy limits plt.xlim(x.min()*0.95, x.max()*1.05) plt.ylim(y.min()*0.95, y.max()*1.05) def hover(event): # check if event was in the axis if event.inaxes = ax: # draw lines and make sure they're visible lnx.set_data(, ) lnx.set_linestyle('-') lny.set_data(, ) lny.set_linestyle('-') lnx.set_visible(True) lny.set_visible(True) # get the points contained in the event cont, ind = sc.contains(event) if cont: # change annotation position annot.xy = (event.xdata, event.ydata) # write the name of every point contained in the event t_text("".format(', '.join( for n in ind]))) t_visible(True) else: t_visible(False) else: lnx.set_visible(False) lny.set_visible(False) _connect("motion_notify_event", hover) plt. When it is, we change the text, position, and visibility of the annotation accordingly. We’ll create a blank annotation and check if the mouse position is over one of the plotted points. Great! We know how to add and modify elements in our plot and detect the movement of the cursor. A third variable can be set to correspond to the color or size of the markers, thus adding yet another dimension to the plot. Each row in the data table is represented by a marker the position depends on its values in the columns set on the X and Y axes. The event we receive from mpl_connect at our hover function has some valuable properties let’s try to get the XY coordinates of the mouse. Scatter plots are used to plot data points on horizontal and vertical axis in the attempt to show how much one variable is affected by another. So even if some element is only displayed when a specific event is triggered, we still should define it outside of our function. But this is not what I am looking for.The idea is to draw the chart with all the elements we’ll use and then use the events to modify those elements. I also tried defining three sets of data, one for each color, and adding them to the plot separately. It creates two random arrays, X and Y, for X-coordinates and Y-coordinates of the points, respectively. xy represents a pair of coordinates (x, y) of the point to be annotated. It annotates the point xy with the value of the text parameter. Why not using a loop to plot the data leads to incorrect color of some points? Add Label to Scatter Plot Points Using the () Function. Plt.scatter(x,y,c=col,s=5, linewidth=0)īefore that, I tried creating the same color list in the same way, but plotting the data without the loop: #scatter plotĮven though this plots the data much, much faster than using the for loop, some of the scattered points appear with a wrong color. the xarray.backends entry point to register additional engines to be used in. And for reasons I don't understand, when I use the function scatter from matplotlib, it plots all points with positives coordinates (it means that. Add () which wraps matplotlibs plotsurface to make. some of these points have negative coordinates (for x and/or y and/or z). To connect these points of scatter plot in order, call (x, y) keeping x and y the same as ones passed into scatter() function. I tried creating a list of the same length as the data with the color I want to assign to each point and then plot the data with a loop, but it takes me a long time to run it. Provide multiple font families, separated by commas, to indicate the preference in which to apply fonts if they arent available on the system. so there is a big trouble in my script : I have done an array with numpy with contains x,y and z coordinates of some points. Call show() After Calling Both scatter() and plot() (x, y) with x as a sequence of x-coordinates and y as a sequence of y-coordinates creates a scatter plot of points. I want to make a scatter plot of them giving each point a different color depending on these conditions: -BLACK if x=10 and y=10 and y>-0.5 I have two numpy arrays, x and y, with 7000 elements each.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |