Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing lines between two plots in Matplotlib

I am drawing two subplots with Matplotlib, essentially following :

subplot(211); imshow(a); scatter(..., ...) subplot(212); imshow(b); scatter(..., ...) 

Can I draw lines between those two subplots? How would I do that?

like image 255
F.X. Avatar asked Jul 09 '13 08:07

F.X.


People also ask

How do I plot a line in Matplotlib?

To plot a line plot in Matplotlib, you use the generic plot() function from the PyPlot instance. There's no specific lineplot() function - the generic one automatically plots using lines or markers. This results in much the same line plot as before, as the values of x are inferred.

How to draw multiple lines in Matplotlib?

To draw multiple lines we will use different functions which are as follows: y = x x = y y = sin(x) y = cos(x) Python3 # importing package importmatplotlib.pyplot as plt importnumpy as np # create data x =[1,2,3,4,5] y =[3,3,3,3,3] # plot lines plt.plot(x, y, label ="line 1") plt.plot(y, x, label ="line 2")

How do you make a line plot in Python?

Line plot: Line plots can be created in Python with Matplotlib’s pyplot library. To build a line plot, first import Matplotlib. It is a standard convention to import Matplotlib’s pyplot library as plt. The plt alias will be familiar to other Python programmers.

How to plot angle values in Matplotlib?

You could use matplotlib.patches.Arc to plot an arc of the corresponding angle measure. Define a function that could take 2 matplotlib.lines.Line2D objects, calculate the angle and return a matplotlib.patches.Arc object, which you can add to your plot along with the lines. To print the angle values :

What is a subplot in Matplotlib?

A subplot () function is a wrapper function which allows the programmer to plot more than one graph in a single figure by just calling it once. Syntax: matplotlib.pyplot.subplots (nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)


1 Answers

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

A better solution would use the specially designed ConnectionPatch:

import matplotlib.pyplot as plt from matplotlib.patches import ConnectionPatch import numpy as np  fig = plt.figure(figsize=(10,5)) ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122)  x,y = np.random.rand(100),np.random.rand(100)  ax1.plot(x,y,'ko') ax2.plot(x,y,'ko')  i = 10 xy = (x[i],y[i]) con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",                       axesA=ax2, axesB=ax1, color="red") ax2.add_artist(con)  ax1.plot(x[i],y[i],'ro',markersize=10) ax2.plot(x[i],y[i],'ro',markersize=10)   plt.show() 

enter image description here

like image 112
ImportanceOfBeingErnest Avatar answered Sep 29 '22 22:09

ImportanceOfBeingErnest