简体   繁体   中英

Plotting time series with seaborn

I have 2 lists. One represents time and looks like

time=[datetime.datetime(2015, 1, 2, 0, 1, 2),datetime.datetime(2015, 1, 2, 0, 5, 2),datetime.datetime(2015, 1, 3, 0, 1, 53),datetime.datetime(2015, 1, 3, 0, 1, 56),datetime.datetime(2015, 1, 5, 0, 1, 2),datetime.datetime(2015, 1, 5, 0, 1, 40),datetime.datetime(2015, 1, 7, 0, 1, 2),datetime.datetime(2015, 1, 7, 0, 1, 30),datetime.datetime(2015, 1, 9, 0, 1, 2),datetime.datetime(2015, 1, 9, 0, 1, 20)]

and the other represents corresponding data points:

data=[51.024,3.2179,105.18,31.176,1.1123,1.7861,109.65,0.0,123.890,523.897]

plotting this with matplotlib is easy, but the rest of the statistics is done with seaborn and I would like to keep the visuals and use seaborn for the whole set of results. When I use seaborn.tsplot I get the following error index contains duplicate entries, cannot reshape seaborn . The data list does contain duplicates, but they are at different time points and cannot be removed. What am I doing wrong?

Edit: if I create pandas dataframe, I can plot the y value using sns.tsplot(y) , but I want to be able to use my values for x-axis, not the generated values.

As an alternative to plotting your data with seaborn, you can use matplotlib 's styles feature to get the same look while still plotting within matplotlib :

from matplotlib import style
# Seaborn's visual styling was inspired by ggplot,
#   so this style should be very similar:
style.use('ggplot')

import matplotlib.pyplot as plt
import datetime
time=[datetime.datetime(2015, 1, 2, 0, 1, 2),datetime.datetime(2015, 1, 2, 0, 5, 2),datetime.datetime(2015, 1, 3, 0, 1, 53),datetime.datetime(2015, 1, 3, 0, 1, 56),datetime.datetime(2015, 1, 5, 0, 1, 2),datetime.datetime(2015, 1, 5, 0, 1, 40),datetime.datetime(2015, 1, 7, 0, 1, 2),datetime.datetime(2015, 1, 7, 0, 1, 30),datetime.datetime(2015, 1, 9, 0, 1, 2),datetime.datetime(2015, 1, 9, 0, 1, 20)]
data=[51.024,3.2179,105.18,31.176,1.1123,1.7861,109.65,0.0,123.890,523.897]

# Replace this with whatever code you were using to plot
#   within matplotib, the styling should still be applied
plt.plot(time, data, 'ro')
plt.show()

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