Plot Multiple Lines in One Graph

How can I plot multiple line in the same graph in python?

You're almost there! You only need to use listt[i] instead of [list1[i] for list1 in listt].

So, the code should look like this:

import matplotlib.pyplot as plt

#random listt
listt = [[i for j in range(200)] for i in range(38)]

generation = []
for i in range(200):
generation.append(i)

plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("A test graph")
for i in range(len(listt)):
plt.plot(generation ,listt[i],label = 'id %s'%i) #<--- change here
plt.legend()
plt.show()

And it returns this graph:
Sample Image

Of course, it won't be exactly as yours since I randomly generated listt.

How to plot multiple lines within the same graph based on multiple subsets

  • For this case, sns.relplot will work
    • seaborn is a high-level API for matplotlib.
  • Given your dataframe data
    • data only contains information where the 'SNAPSHOT' year is 2020, however, for the full dataset, there will be a row of plots for each year in 'Snapshot_Year'.
  • Since the x-axis will be different for each row of plots, facet_kws={'sharex': False}) is used, so xlim can scale based on the date range for the year.
import pandas as pd
import seaborn as sns

# convert SNAPSHOT_DATE to a datetime dtype
data.SNAPSHOT_DATE = pd.to_datetime(data.SNAPSHOT_DATE)

# add the snapshot year as a new column
data.insert(1, 'Snapshot_Year', data.SNAPSHOT_DATE.dt.year)

# plot the data
g = sns.relplot(data=data, col='DEPLOYMENT_TYPE', row='Snapshot_Year', x='SNAPSHOT_DATE', y='TOTAL_WIDGETS',
hue='FORECAST_YEAR', kind='line', facet_kws={'sharex': False})
g.set_xticklabels(rotation=90)
plt.tight_layout()

Sample Image

Plotly How to plot multiple lines with different X-arrays on the same Y-axis

When you don't have a dataframe, I argue that the easiest way is to use plotly.graph_objects.

import plotly.graph_objects as go

x1 = [1, 3, 5, 7, 9]
y1 = np.random.random(5)
x2 = [2, 4, 6, 8, 10]
y2 = np.random.random(5)

f1 = go.Figure(
data = [
go.Scatter(x=x1, y=y1, name="first"),
go.Scatter(x=x2, y=y2, name="second"),
],
layout = {"xaxis": {"title": "x axis"}, "yaxis": {"title": "y axis"}, "title": "My title"}
)
f1

Sample Image

You can also use the plotly.express module, however it requires a bit more code to set the names on the legends.

Learn more at this documentation page.

Tableau - Plotting multiple lines in one graph

Since you have mentioned that you want to show multiple measures in one viz it means that one axis is sufficient for all these measures. Therefore, Instead of creating dual axis chart, do it like this-

Step-1 Build line chart with one measure.

Step-2 Drop another measure on the axis directly instead of rows/column shelf where you'll see a double bar icon like the screenshot.

Sample Image

step-3 Drop additional mesaures either in similar ways or onto measure values pane created automatically. See a demonstration screenshot below on sample superstore

Sample Image



Related Topics



Leave a reply



Submit