简体   繁体   中英

Matplotlib/Seaborn spaghetti plot - Show None values

I have such a dataframe:

frame = {'Date' : ['3-Mar', '20-Mar', '20-Apr', '21-Apr', '29-Apr', '7-May', '30-May', '31-May', '7-Jun', '16-Jun',
        '1-Jul', '2-Jul', '10-Jul'],
        'Test_1' : [0.5840, 0.8159, 0.7789, 0.7665, 0.8510, 0.7428, 'None', 0.6820, 0.8714, 0.8902, 'mraky', 0.8289, 0.6877],
        'Test_2' : [0.6196, 0.8291, 0.7686, 0.7848, 0.9935, 0.7406, 'None', 0.6952, 0.6952, 0.6952, 'None', 0.8119, 'None']}

And additional arrays of tests with some none values. I would like to do a spaghetti plot.

The issue is that seaborn shuffle 'date' values/items when they are in the form of a string (they are not chronologically ordered) or if I changed it to date formate by:

frame['Date'] =  pd.to_datetime(frame['Date'], format='%d-%b', errors='ignore')

The seaborn skip some entries and do something like this: 在此处输入图片说明

Yeah I could probably replace the None values by the mean of adjacent values, but is there another way?

THX!

So it looks like you are trying to do a regular plot from two different time series.

It also looks like the DF has non numerical values within it.

Now its unclear if you want a continuous plot (line) or if you want it to "break" on points where you have no numerical data.
To keep the solution simple, lets assume you dont mind a continuous line.

So, what you need to do is the following,

  • select only data points with numerical values

Define a filter function:

def is_digit (value):

    try:

        float(value)
        return True

    except:

        return False

Apply it to your dataframe:

data_1 = df.loc[df.Test_1.map(lambda X: is_digit(X))]
data_2 = df.loc[df.Test_2.map(lambda X: is_digit(X))]

Plot:

X1 = data_1.Date
y1 = data_1.Test_1

X2 = data_2.Date
y2 = data_2.Test_2

plt.plot(X1,y1)
plt.plot(X2,y2)

plt.xticks(rotation=45)

Sample (also ugly) result:

在此处输入图片说明

Possible improvements:

Making a date array, of even intervals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM