Kallol
Kallol

Reputation: 2189

Plot two line graphs in same figure with different colours and x and y axis ranges are fixed

I have two lists like this

l1= [2, 8, 5, 19, 15, 23]
l2= [3, 5, 8, 11, 14, 50]

Now I want to plot line graph this where the Y axis will be having range 0 to 50 and X axis will be having range from 0 to 50. both l1 and l2 lines will be having different colour. How to do this using matplotlib or seaborn ?

Upvotes: 0

Views: 755

Answers (2)

Nk03
Nk03

Reputation: 14949

  • Load the list into pandas dataframe.
  • Restructure the dataframe
  • Plot via seaborn.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

l1= [2, 8, 5, 19, 15, 23]
l2= [3, 5, 8, 11, 14, 50]

df = pd.DataFrame({'l1': l1, 'l2': l2}).stack().reset_index(name='val').assign(level_0 = lambda x: x.level_0 * 10)
sns.set_style('whitegrid')
fg = sns.lineplot(x="level_0", y = "val" , hue="level_1", data=df,  ci=None, palette='Set1')

plt.savefig('test5.png')

OUTPUT:

enter image description here

Upvotes: 2

dr-magic
dr-magic

Reputation: 1

A simple solution would be to use matplotlib.pyplot directly.

import matplotlib.pyplot as plt
    
l1= [2, 8, 5, 19, 15, 23]
l2= [3, 5, 8, 11, 14, 50]

plt.ylim((0, 50))  # set Y axis range to 0..50
plt.xlim((0, 50))  # set X axis range to 0..50
plt.plot(l1, color='red')  # plot l1
plt.plot(l2, color='blue') # plot l2
plt.show()

See matplotlib.pyplot.plot() for further styling options.

Upvotes: 0

Related Questions