0

I have two lists representing dates and values respectively:

dates = ['10/6/2020',
 '10/7/2020',
 '10/8/2020',
 '10/9/2020',
 '10/12/2020',
 '10/13/2020',
 '10/14/2020',
 '10/15/2020',
 '10/16/2020',
 '10/19/2020']

and

values = ['40.660',
 '39.650',
 '41.010',
 '41.380',
 '39.950',
 '40.790',
 '41.050',
 '40.370',
 '40.880',
 '40.860']

I want to use seaborn/matplotlib to plot them without using pandas. Is that possible? I've made a few attempts but it doesn't seem to be going too well.

Here's what I've got so far:

def plots(values=values,dates=dates):
    sns.lineplot(x=dates,y=sorted(values)[::-1])
    sns.scatterplot(x=dates,y=sorted(values)[::-1])
    plt.show()
    return 

crude_data = plots()

But it gives me this:

enter image description here

This is obviously wrong but I don't know how to fix it. The x-axis is also messy, and I'd like to fix that as well and make it more legible without expanding the width of the graph if possible. If that's impossible, then a way to do so while expanding the graph would be happily accepted as well.

Cheers!

2
  • It happens because you're sorting the values. Commented Oct 31, 2020 at 1:14
  • @RnRoger thanks for the advice. I tried sns.lineplot(x=dates,y=values) and sns.scatterplot(x=dates,y=(values)) but that gave me the same as above. Commented Oct 31, 2020 at 1:20

3 Answers 3

2

Just parse dates from string to datetime.

Otherwise they are treated as strings and lose all their properties besides sortability (which can be wrong, depending on date format).

Also add xticks rotation for better x axis labels.
edit: Also as others noticed, your numeric data is in string type aswell.

import seaborn as sns
import matplotlib.pyplot as plt

import datetime

dates = ['10/6/2020',
 '10/7/2020',
 '10/8/2020',
 '10/9/2020',
 '10/12/2020',
 '10/13/2020',
 '10/14/2020',
 '10/15/2020',
 '10/16/2020',
 '10/19/2020']

values = ['40.660',
 '39.650',
 '41.010',
 '41.380',
 '39.950',
 '40.790',
 '41.050',
 '40.370',
 '40.880',
 '40.860']

dates = [datetime.datetime.strptime(date, '%m/%d/%Y') for date in dates]
values = [float(val) for val in values]

def plots(values=values,dates=dates):
    sns.lineplot(x=dates,y=sorted(values)[::-1])
    sns.scatterplot(x=dates,y=sorted(values)[::-1])
    plt.xticks(rotation=45)
    plt.show()
    return crude_data = plots()

enter image description here

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

Comments

1

You can use datetime library.

import matplotlib.pyplot as plt
from datetime import datetime
import seaborn as sns

dates = ['10/6/2020',
 '10/7/2020',
 '10/8/2020',
 '10/9/2020',
 '10/12/2020',
 '10/13/2020',
 '10/14/2020',
 '10/15/2020',
 '10/16/2020',
 '10/19/2020']

x = [datetime.strptime(i, '%m/%d/%Y') for i in dates]

values = ['40.660',
 '39.650',
 '41.010',
 '41.380',
 '39.950',
 '40.790',
 '41.050',
 '40.370',
 '40.880',
 '40.860']

nvalues = [float(i) for i in values]

def plots(values=nvalues,dates=x):
    sns.lineplot(x=dates,y=sorted(values)[::-1])
    sns.scatterplot(x=dates,y=sorted(values)[::-1])
    plt.xticks(rotation=45, ha='right')
    plt.show()
    return 
import matplotlib.pyplot as plt

from datetime import datetime


crude_data = plots()

Output:

enter image description here

Comments

0

I made 3 changes to your code:

I set the y axis data to be float instead of string by removing the '', I removed sorted from the two plot lines, and I added sort=False to the lineplot call

import matplotlib.pyplot as plt
import seaborn as sns


dates = ['10/6/2020',
 '10/7/2020',
 '10/8/2020',
 '10/9/2020',
 '10/12/2020',
 '10/13/2020',
 '10/14/2020',
 '10/15/2020',
 '10/16/2020',
 '10/19/2020']


values = [40.660,
 39.650,
 41.010,
 41.380,
 39.950,
 40.790,
 41.050,
 40.370,
 40.880,
 40.860]



def plots(values=values,dates=dates):
    sns.lineplot(x=dates,y=values[::-1], sort=False)
    s = sns.scatterplot(x=dates,y=values[::-1])
    plt.xticks(rotation=45, ha='right')
    plt.show()
    return s

crude_data = plots()

This gives:

chart

Comments

Your Answer

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