Pyrmon55
Pyrmon55

Reputation: 193

Plot two lists with different length

I have two lists with different prices. The first list is for the years 2008-2018 and the second for the years 2010-2018. How I can plot them under the condition that the years 2008 to 2018 are on the X-axis and the second list starts in 2010?

I have the following as an example of a short code:

from matplotlib import pyplot as plt

Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
Geb_a30 = [12, 10, 13, 14, 12, 13, 18, 16]

fig, ax = plt.subplots()
ax.plot(Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(Geb_a30, label='Prices 2010-2018', color = 'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()

Upvotes: 8

Views: 30899

Answers (5)

Mad Physicist
Mad Physicist

Reputation: 114250

To tell matplotlib where you want points to end up on the x-axis, you must explicitly provide the x-values. The size of the x-axis values must correspond to the size of the y-values, but there does not need to be any relationship between sets of independent data, as you've already seen.

Geb_x = range(2008, 2018)

...

ax.plot(Geb_x, Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(Geb_x[2:], Geb_a30, label='Prices 2010-2018', color = 'red')

Upvotes: 4

andrew_reece
andrew_reece

Reputation: 21264

IIUC, just pad your missing years with None/NaN:

import pandas as pd

years = list(range(2008, 2018))
Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
Geb_a30 = [None, None, 12, 10, 13, 14, 12, 13, 18, 16]
df = pd.DataFrame({"years":years, "b30": Geb_b30, "a30": Geb_a30})

df.plot(x="years")

plot

Upvotes: 5

Batman
Batman

Reputation: 8917

There are a lot of ways you could achieve this. One elegant way would be to use pandas. This way you automatically get correctly labelled and aligned x ticks.

from matplotlib import pyplot as plt
import pandas as pd

geb_b30_x = pd.date_range(start="20080101", end="20180101", freq="A")
geb_b30_y = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
geb_b30 = pd.Series(data=geb_b30_y, index=geb_b30_x)

geb_a30_x = pd.date_range(start="20100101", end="20180101", freq="A")
geb_a30_y = [12, 10, 13, 14, 12, 13, 18, 16]
geb_a30 = pd.Series(data=geb_a30_y, index=geb_a30_x)

fig, ax = plt.subplots()
ax.plot(geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(geb_a30, label='Prices 2010-2018', color = 'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()

Upvotes: 1

VegardKT
VegardKT

Reputation: 1246

you should create a new list containing your years. Then you can specify where on the x-axis you want to plot by odoing years[10:18] for instance

from matplotlib import pyplot as plt

Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
Geb_a30 = [12, 10, 13, 14, 12, 13, 18, 16]

years = list(range(2008,2018))

fig, ax = plt.subplots()
ax.plot(years[0:len(Geb_b30)],Geb_b30, label='Prices 2008-2018', 
color='blue')
ax.plot(years[2:],Geb_a30, label='Prices 2010-2018', color = 
'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()

EDIT: Updated with correct x-axis

Upvotes: 4

Laurent H.
Laurent H.

Reputation: 6526

I suggest you to simply define the x values (i.e. the list of years) for each set of points, and to pass them in parameters of ax.plot(), as follows:

from matplotlib import pyplot as plt

Geb_b30 = [11, 10, 12, 14, 16, 19, 17, 14, 18, 17]
years_b30 = range(2008,2018)
Geb_a30 = [12, 10, 13, 14, 12, 13, 18, 16]
years_a30 = range(2010,2018)

fig, ax = plt.subplots()
ax.plot(years_b30, Geb_b30, label='Prices 2008-2018', color='blue')
ax.plot(years_a30, Geb_a30, label='Prices 2010-2018', color = 'red')
legend = ax.legend(loc='center right', fontsize='x-large')
plt.xlabel('years')
plt.ylabel('prices')
plt.title('Comparison of the different prices')
plt.show()

Upvotes: 16

Related Questions