Python Forum

Full Version: plotting 2 different set of data in a single plot with shared axes.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi..
I have 2 different sets of data like, x1=[1,2,3,4,5],y1=[some corresponding data] and x2=[6,7,8,9,10],y2=[some corresponding data].
How can i plot these in a single plot with shared axes?

to be specific, these are my data:
x1=[1, 3, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4, 1, 5, 2, 3, 4]
y1=[1, 1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 30, 31, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 50, 51, 52, 53, 54, 54, 55, 56, 57, 58, 58, 59, 60, 61, 62, 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 70, 71, 72, 73, 74, 74, 75, 76, 77, 78, 78, 79, 80, 81, 82, 82, 83, 84, 85, 86, 86, 87, 88, 89, 90, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 98, 99, 100, 101, 102, 102, 103, 104]

x2=[6, 9, 13, 7, 11, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 8, 13, 6, 9, 10, 7, 11, 12, 10, 7, 11, 12, 10, 7, 11, 12, 10, 12, 11, 7, 9, 8, 11, 11, 12, 10, 10, 12, 7, 10, 11, 8, 8, 11, 10, 7, 11, 8, 11, 12, 7, 12, 7, 11, 7, 12, 10, 12]

y2=[1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 10, 10, 11, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 14, 15, 15, 16, 16, 17, 17, 17, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 22, 22, 23, 23, 23, 23, 24, 24, 25, 25, 26, 26, 26, 26, 27, 27, 28, 28, 29, 29, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 34, 34, 35, 35, 35, 35, 36, 36, 37, 37, 38, 38, 38, 38, 39, 39, 40, 40, 41, 41, 41, 41, 42, 42, 43, 43, 44, 44, 44, 44, 45, 45, 46, 46, 47, 47, 47, 47, 48, 48, 49, 49, 50, 50, 50, 50, 51, 51, 52, 52, 53, 53, 53, 53, 54, 54, 55, 55, 56, 56, 56, 56, 57, 57, 58, 58, 59, 59, 59, 59, 60, 60, 61, 61, 62, 62, 62, 62, 63, 63, 64, 64, 65, 65, 65, 65, 66, 66, 67, 67, 68, 68, 68, 68, 69, 69, 70, 70, 71, 71, 71, 71, 72, 72, 73, 73, 74, 74, 74, 74, 75, 75, 76, 76, 77, 77, 77, 77, 78, 78, 79, 79, 80, 80, 80, 80, 81, 81, 82, 82, 83, 83, 83, 83, 84, 84, 85, 85, 86, 86, 86, 86, 87, 87, 88, 88, 89, 89, 89, 89, 90, 90, 91, 91, 92, 92, 92, 92, 93, 93, 94, 94, 95, 95, 95, 95, 96, 96, 97, 97, 98, 98, 98, 98, 99, 99, 100, 100, 101, 101, 101, 101, 102, 102, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 106, 106, 106, 106, 107, 107, 107, 108, 108, 108, 109, 109, 109, 110, 110, 110, 110, 111, 111, 111, 111, 112, 112, 112, 113, 113, 113, 114, 114]


please help...
What have you tried? Have you looked at Matplotlib?
(Mar-21-2018, 12:23 PM)sparkz_alot Wrote: [ -> ]What have you tried? Have you looked at Matplotlib?
Yea i did.

fig=plt.figure()
ax=fig.add_subplot(111,label="1")
ax2=fig.add_subplot(111,label="2",frame_on=False)
ax.plot([t1],[i1],marker='.',markersize=3,color='blue')
ax.set_xlabel("Time (ms)-id2", color='blue')
ax.set_ylabel("Neuron Index-id2", color='blue')
ax.tick_params(axis='x', colors='blue')
ax.tick_params(axis='y', colors='blue')
ax2.plot([t2],[i2],marker='.',markersize=3,color='red')
ax2.xaxis.tick_top()
ax2.yaxis.tick_right()
ax2.set_xlabel('Time (ms)-id 3', color="red") 
ax2.set_ylabel('Neuron Index_id 3', color="red")       
ax2.xaxis.set_label_position('top') 
ax2.yaxis.set_label_position('right') 
ax2.tick_params(axis='x', colors="red")
ax2.tick_params(axis='y', colors="red")
doing like this i get inter-digitated plot. ie, the second axis(for x2 and y2) starts with 6.
I want to plot these with a single shared axis rather than 2 axes.

something like this.
x1 = [1,2,3,4,5]
x2 = list(reversed(x1))
y1 = [10, 20, 30, 40, 50]
y2 = [100, 200, 300, 400, 500]

# x and y must have the same length.

plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()
Matplotlib seems to make the mapping automatically.
(Mar-21-2018, 01:29 PM)DeaD_EyE Wrote: [ -> ]x1 = [1,2,3,4,5]
x2 = list(reversed(x1))
y1 = [10, 20, 30, 40, 50]
y2 = [100, 200, 300, 400, 500]

# x and y must have the same length.

plt.plot(x1, y1)
plt.plot(x2, y2)
plt.show()

Thank you so much...its working.. I seemed to have complicated the code too much.