MikiBelavista
MikiBelavista

Reputation: 2738

How to plot two data sets with different number of points over same time scale?

This is my code

import matplotlib.pyplot as plt

years = [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020]
ic = [200, 270, 386, 520, 720, 1180, 2110, 4764, 5834, 6832, 7972, 8933, 10897, 12635, 21443] 
pe = [38035, 49261, 55709, 67246, 73549]

fig = plt.figure()

plt.plot(years, ic,  lw=2, marker='o')
#plt.plot(years, pe,  lw=2, marker='s')
plt.xlabel('Years')
plt.ylabel('Installed Capacity MWe')
plt.grid()
plt.tight_layout()
plt.show() 

Image is what I want for first data set enter image description here

I have Pruduced Energy only from 1995-2015.That is my pe

How to add this data set on the same plot?

Upvotes: 0

Views: 2357

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339430

Of course the number of values to plot for x and y needs to be the same and there needs to be a one-to-one correspondence between them. Hence you need a new years dataset to plot against.

import matplotlib.pyplot as plt

years1 = [1950, 1955, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015, 2020]
ic = [200, 270, 386, 520, 720, 1180, 2110, 4764, 5834, 6832, 7972, 8933, 10897, 12635, 21443] 

years2 = [1995, 2000, 2005, 2010, 2015]
pe = [38035, 49261, 55709, 67246, 73549]

fig = plt.figure()

plt.plot(years1, ic,  lw=2, marker='o')
plt.plot(years2, pe,  lw=2, marker='s')
plt.xlabel('Years')
plt.ylabel('Installed Capacity MWe')
plt.grid()
plt.tight_layout()
plt.show() 

enter image description here

Upvotes: 2

Related Questions