'Plot panda dataframe with multiple x and y columns
I want to do a pandas dataframe plot and plot x1 with y1 and x2 with y2 in one plot. My dataframe looks something like this:
import pandas as pd
d = {'x1': [1,2,3,4,5], 'x2': [2,4,8,10,11], "y1" : [3,3,3,3,3], "y2" : [4,3,2,1,2]}
df = pd.DataFrame(data=d)
df.plot(x = "x1", y = "y1")
df.plot(x = "x2", y = "y2")
However, I always obtain two different plots. I tried something like
df.plot(x = ["x1", "x2"], y = ["y1", "y2"])
but I realized, that x must be a label or position. I know from Matlab a hold on solution, but I am not sure how to do it in pandas.
Solution 1:[1]
IIUC use:
ax = df.plot(x = "x1", y = "y1")
df.plot(x = "x2", y = "y2", ax=ax)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezrael |
