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:
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 workseaborn
is a high-level API formatplotlib
.
- 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, soxlim
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()
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
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.
step-3 Drop additional mesaures either in similar ways or onto measure values pane created automatically. See a demonstration screenshot below on sample superstore
Related Topics
Clang-7: Error: Linker Command Failed With Exit Code 1 For Macos Big Sur
Read All Worksheets in an Excel Workbook into an R List With Data.Frames
Summarizing Multiple Columns With Data.Table
Manually Setting Group Colors For Ggplot2
Pass Arguments to Dplyr Functions
How to Use Facets With a Dual Y-Axis Ggplot
Why Is the Parallel Package Slower Than Just Using Apply
Chopping a String into a Vector of Fixed Width Character Elements
Summarizing by Subgroup Percentage in R
Reasons For Using the Set.Seed Function
How Can One Work Fully Generically in Data.Table in R With Column Names in Variables
Limit Ggplot2 Axes Without Removing Data (Outside Limits): Zoom
Consistent Width For Geom_Bar in the Event of Missing Data
Select Equivalent Rows [A-B & B-A]
Workflow For Statistical Analysis and Report Writing
Construct a Manual Legend For a Complicated Plot