Search code examples
pythonmatplotlibscatter-plotline-plot

Can't visualize using a line plot


I've tested matlpotlib using CSV file, which y and z value isn't connected as below.

I can visualize this data using a scatter plot but I can't connect these dots but the final goal is creating a connected graph using the line plot. Why it can't visualize with the line plot?

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv(r'C:\Users\PythonTest\source\file.csv')
ax1 = plt.subplot2grid((1, 1), (0, 0))
x = df.time
y = df.gx
z = df.sp
# ax1.scatter(x, y, color='k', s=0.1)
# ax1.scatter(x, z, color='c', s=0.1)
ax1.plot(x, y, color='k')
ax1.plot(x, z, color='c')
plt.show()

Solution

  • You can scatter plot discontinuous values, but you cannot plot a line of discontinuous values because matplotlib cannot magically know what values you want in between specified values. Do you want to treat missing values as 0? As the previous value? Something else?

    One way to accomplish what I think you probably want is to drop the missing values. Let's do time and gx:

    time_gx = df.drop(columns=['sp']).dropna()
    plt.plot(time_gx['time'], time_gx['gx'])
    plt.xlabel('time')
    plt.ylabel('gx')
    plt.show()
    

    enter image description here

    If instead you want to treat missing values as the last specified value, just use ffill:

    df2 = df.ffill()
    plt.plot(df2['time'], df2['gx'])
    plt.xlabel('time')
    plt.ylabel('gx')
    plt.show()
    

    enter image description here