![]() With a string index, you can try something like this from matplotlib import pyplot as plt I wrongly assumed your index was a numeric one. Lines = LineCollection(, points_vs_xpoints.index],, points_vs_xpoints.index]] for i, el in enumerate(zip(points_vs_xpoints,points_vs_xpoints))], label='Connection', linestyle='dotted') from matplotlib import pyplot as pltįrom llections import LineCollectionĪx.plot(points_vs_xpoints,points_vs_xpoints.index, label="Projected", linestyle = 'None', marker="o")Īx.plot(points_vs_xpoints, points_vs_xpoints.index, label="Actual", linestyle = 'None', marker="o", markeredgecolor="r", markerfacecolor='None') If I understood your request correctly, this should do the trick. How to plot a new line between the markers on two separate plot lines using matplotlib? In my opinion, this is simple enough (4 lines): fig, ax = plt.subplots() How to draw a line between a data point and an axis in matplotlib? So, let's set the linestyle: klicker = clicker(ax,, markers=, **) line_kwargs will be passed through to all of the ax.plot calls. **line_kwargs (kwargs) – Line2D objects (from ax.plot) are used to generate the markers. How to draw lines between mouseclicks on a matplotlib plot? In above code the line would begin at the start of every figure edge, and it will change when the figure size changed. Line = Line2D(,, color='w', transform=trans) trans = blended_transform_factory(, ansAxes) I tried another way which I think is the most perfect way. ot(,, color='black', lw=1, transform=ansAxes, clip_on=False)Īxes.plot(,, color='black', lw=1, transform=ansAxes, clip_on=False) Where are values suppose to cover all areas of X axis in figure. I find a solution but not a perfect one, but works for me.Īpply below code to each object of subplot. import matplotlib.pyplot as pltįig, (ax1, ax2) = plt.subplots(1,2, sharex=True, sharey=True) The good thing about this ConnectionPatch is not only that it is easy to realize, but also it will move and zoom together with the data. ![]() Plot a line between two subplots while handling the zoom with MatplotlibĪs the comment in the linked question (Drawing lines between two plots in Matplotlib) suggests, you should use a ConnectionPatch to connect the plots. ![]() X,y = np.random.rand(100),np.random.rand(100)Ĭon = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data", The solution from the other answers are suboptimal in many cases (as they would only work if no changes are made to the plot after calculating the points).Ī better solution would use the specially designed ConnectionPatch: import matplotlib.pyplot as pltįrom matplotlib.patches import ConnectionPatch But then both calls to ax.legend would have required some extra arguments so that each legend contained only the items we wanted.Drawing lines between two plots in Matplotlib We could have, alternatively, given labels to the markers when they were plotted too. The legend for the markers ( leg2) therefore required the lines and labels as arguments when it was instantiated. ![]() For example, add the following to the bottom of the above code: leg1.get_lines().set_lw(8)įinally, it's worth mentioning that in the example only the lines were given labels when plotted, meaning that ax.legend() adds only those lines to the leg1. The really great thing is that you can can still manipulate both legends. The first one you instantiate ( leg1) is removed from the figure when you add the second one, but the leg1 object still exists and can be added back with ax.add_artist. The key is to make sure you have references to the legend objects. # Add second legend for the maxes and mins. # Add first legend: only labeled data is included # Plot maxima and minima, and keep references to the lines One legend is for the lines, and the other is for the markers. In the example here, we plot two lines, then plot markers on their respective maxima and minima. This has been done so that it is possible to call legend() repeatedly to update the legend to the latest handles on the Axesįear not, though: It is still quite simple to add a second legend (or third, or fourth.) to an axes. If you call plt.legend() or ax.legend() more than once, the first legend is removed and a new one is drawn.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |