Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different plots from same loop in matplotlib?

I would specifically like to create two different plots using one single loop. One plot should have four straight lines from x-y, and another plot should have four angled lines from x-y2. My code only shows everything in a single plot. I don't quite understand how plt works, how can I create two distinct plt objects?

import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2

x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']

for i in range(len(x)):
   plt.plot(x[i],y2[i],colours[i])
   plt2.plot(x[i],y[i],colours[i])

plt.show()
plt2.show()
like image 906
KubiK888 Avatar asked Oct 23 '15 18:10

KubiK888


People also ask

How do I show multiple plots in matplotlib?

To draw multiple plots using the subplot() function from the pyplot module, you need to perform two steps: First, you need to call the subplot() function with three parameters: (1) the number of rows for your grid, (2) the number of columns for your grid, and (3) the location or axis for plotting.

Can I have two legends in matplotlib?

Sometimes when designing a plot you'd like to add multiple legends to the same axes. Unfortunately, Matplotlib does not make this easy: via the standard legend interface, it is only possible to create a single legend for the entire plot. If you try to create a second legend using plt. legend() or ax.


1 Answers

Is that what you want to do?

import matplotlib.pyplot as plt

x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
y2=[[11,12,13,24],[42,33,34,65],[23,54,65,86],[77,90,39,54]]
colours=['r','g','b','k']

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
for i in range(len(x)):
    ax1.plot(x[i],y2[i],colours[i])
    ax2.plot(x[i],y[i],colours[i])

fig1.show()
fig2.show()
like image 88
cel Avatar answered Oct 28 '22 12:10

cel