0

I have been working on Matplotlib to plot different datasets into same plot(say a comparision line plot) with each and every datasets have different x-axis and y-axis values.

for example dataset will look like:

x =  [['12.63', '13.50', '14.15', '15.18', '16.04', '17.28', '18.56', '19.70',
       '20.90', '22.21', '23.25', '24.13'],
      ['13.39', '14.10', '15.05', '16.20', '17.55', '18.43', '19.75', '21.29',
       '22.78', '24.00', '24.85', '24.81'],
      ['13.02', '13.86', '14.82', '15.80', '16.90', '17.99', '19.24', '20.79',
       '22.30', '23.43', '24.38', '24.68']]

y = [['-15.09', '-15.19', '-15.23', '-15.32', '-15.07', '-15.11', '-15.04',
      '-15.08', '-14.97', '-14.98', '-14.89', '-15.12'],
     ['-15.91', '-15.89', '-15.90', '-15.96', '-15.55', '-15.58', '-15.51',
      '-15.48', '-15.42', '-15.40', '-15.85', '-16.64'],
     ['-15.71', '-15.75', '-15.83', '-15.83', '-15.54', '-15.55', '-15.53',
      '-15.47', '-15.41', '-15.33', '-15.43', '-15.97']]

How do i make the comparison chart like below example (refernce from google)

enter image description here

1 Answer 1

1

First, your data is string. You should convert them into floats.

Then, the easiest way to do is to call plot function multiple times, one for each line:

x =  [['12.63', '13.50', '14.15', '15.18', '16.04', '17.28', '18.56', '19.70',
       '20.90', '22.21', '23.25', '24.13'],
      ['13.39', '14.10', '15.05', '16.20', '17.55', '18.43', '19.75', '21.29',
       '22.78', '24.00', '24.85', '24.81'],
      ['13.02', '13.86', '14.82', '15.80', '16.90', '17.99', '19.24', '20.79',
       '22.30', '23.43', '24.38', '24.68']]

y = [['-15.09', '-15.19', '-15.23', '-15.32', '-15.07', '-15.11', '-15.04',
      '-15.08', '-14.97', '-14.98', '-14.89', '-15.12'],
     ['-15.91', '-15.89', '-15.90', '-15.96', '-15.55', '-15.58', '-15.51',
      '-15.48', '-15.42', '-15.40', '-15.85', '-16.64'],
     ['-15.71', '-15.75', '-15.83', '-15.83', '-15.54', '-15.55', '-15.53',
      '-15.47', '-15.41', '-15.33', '-15.43', '-15.97']]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([float(n) for n in x[0]], [float(n) for n in y[0]], color='tab:blue')
ax.plot([float(n) for n in x[1]], [float(n) for n in y[1]], color='tab:orange')
ax.plot([float(n) for n in x[2]], [float(n) for n in y[2]], color='tab:red')
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help. Is there a way were i can plot for N number(arbitrary) number of datasets) of datasets. As i will have more than 3 x and y datasets each time
put it in a loop: for m in range(number_of_datasets): ax.plot([float(n) for n in x[m]], [float(n) for n in y[m]])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.