3

Below is my code Snippet. I would like to plot each column at a time against time in a line plot. Each column in different plots But the plot seems to keep accumulating the older values in the bottom.

import os
import pandas as pd
import numpy as np
from matplotlib import pyplot
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()


import re
def makeLinePlot(df):
    file_name="File Name not passed to this method"
    print("Ploting")
    df2=df
    fig,ax=plt.subplots(figsize=(30,8))
    i=0
    numRos = df.columns.size
    for colName in df.columns:
        i=i+1
        print(file_name,colName)
        df2[colName]=df2[colName].astype(float)
        print(colName,df2[colName].shape)
        g=sns.lineplot(x=df2.index, y=df2[colName], ax=ax)
        colNameR = re.sub('\W+','', colName )
        g.get_figure().savefig(colNameR+".png")


range = pd.date_range('2015-01-01', '2015-01-02', freq='1min')
df = pd.DataFrame(index = range)

# Average speed in miles per hour
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
# Distance in miles (speed * 0.5 hours)
df['distance'] = df['speed'] * 0.25 
# Cumulative distance travelled
df['cumulative_distance'] = df.distance.cumsum()
makeLinePlot(df)

enter image description here

3
  • Yes, due to ax=ax both lineplots are plotted to the same axes. So you need to create two figures, and hence two axes and then you can plot each line to its own ax. Commented Jul 6, 2019 at 18:43
  • How to fix it.. Commented Jul 6, 2019 at 22:17
  • You need fig,ax = ... inside the loop. Commented Jul 6, 2019 at 22:21

1 Answer 1

1

I have made some changes in your code and now it seems I have got what you want. Use the following code.

import os
import pandas as pd
import numpy as np
from matplotlib import pyplot
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()


import re
def makeLinePlot(df):
    df2=df
    i=0

    numRos = df.columns.size

    fig,ax =plt.subplots( nrows=1, ncols=numRos,figsize=(30,8))

    for index, colName in enumerate(df.columns):
        i=i+1
        df2[colName]=df2[colName].astype(float)
        print(colName,df2[colName].shape)
        g=sns.lineplot(x=df2.index, y=df2[colName], ax=ax[index])
        colNameR = re.sub('\W+','', colName )
        g.get_figure().savefig(colNameR+".png")


range = pd.date_range('2015-01-01', '2015-01-02', freq='1min')
df = pd.DataFrame(index = range)

# Average speed in miles per hour
df['speed'] = np.random.randint(low=0, high=60, size=len(df.index))
# Distance in miles (speed * 0.5 hours)
df['distance'] = df['speed'] * 0.25 
# Cumulative distance travelled
df['cumulative_distance'] = df.distance.cumsum()
makeLinePlot(df)

I have got this. enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.