Home > OS >  How to plot columns with different values but at the same scale with pandas?
How to plot columns with different values but at the same scale with pandas?

Time:09-26

How to plot the two curves representing value1 and value2 columns according to time columns (x-axis) on the same graph?

import pandas as pd

data1 = {'time': [0.973841, 1.000934, 1.027918, 1.055180, 1.082246],
        'value1': [0.483, 0.494, 0.468, 0.477, 0.460]}

data2 = {'time': [0.779984, 0.801439, 0.823164, 0.844759, 0.866461],
        'value2': [0.027, 0.036, 0.030, 0.030, 0.035]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

Also how to make it if one of the dataframes does not have the same length?

data3 = {'time': [0.779984, 0.801439, 0.823164, 0.844759, 0.866461, 1.0, 2.0],
        'value3': [0.027, 0.036, 0.030, 0.030, 0.035, 0.04, 0.045]}

CodePudding user response:

Plot first df which returns an Axis object, then re-use that object. Not need to have dataframes of the same length.

from matplotlib import pyplot as plt
ax = df1.plot(x='time')
df2.plot(x='time', ax=ax)
plt.show()

figure

  • Related