海底情节-时间顺序

时间:2019-03-06 12:33:24

标签: python dataframe plot seaborn

我从数据框中获得了这样的Seaborn意大利面条图。如您所见-日期(以字符串形式;第一列是获取的日期;另一列-不同的测量场所)被打乱了,我想按时间顺序将其放在数据框中。我在网上寻找一些提示,但没有成功。

enter image description here

这是我重要的代码部分:

# library and packeges
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import pandas as pd
import numpy as np
import seaborn as sns
import os
from datetime import datetime


# Data test set for year 2015; 8 rapeseed fields.
data = {'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'],
        'Field_1' : [0.5840, 0.8159, 0.7789, 0.7665, 0.8510, 0.7428, 'mraky', 0.6820, 0.8714, 0.8902, 'mraky', 0.8289, 0.6877],
        'Field_2' : [0.6196, 0.8291, 0.7686, 0.7848, 0.9935, 0.7406, 'mraky', 0.6952, 0.6952, 0.6952, 'mraky', 0.8119, 'mraky'],
        'Field_3' : [0.6790, 0.9220, 0.8380, 0.8312, 'mraky', 0.7398, 'mraky', 0.7019, 0.7985, 0.8941, 0.6430, 0.7891, 'mraky'],
        'Field_4' : [0.5570, 0.8285, 0.8084, 0.8340, 0.9095, 'mraky', 'mraky', 0.6675, 0.8648, 0.9536, 0.7933, 0.7641, 0.6566],
        'Field_5' : [0.6085, 0.8404, 0.8036, 0.8009, 'mraky', 0.7811, 0.5960, 0.6685, 0.8815, 'mraky', 'mraky', 0.8438, 0.6959],
        'Field_6' : [0.5187, 0.7611, 0.7920, 0.7935, 0.8554, 0.7151, 0.7929, 0.6657, 0.8697, 0.8842, 0.8055, 0.8304, 0.6531],
        'Field_7' : [0.4110, 0.5756, 0.5911, 0.5538, 0.6688, 0.7373, 0.6962, 0.6345, 0.8210, 0.8376, 0.7750, 0.8033, 0.5736],
        'Field_8' : [0.5225, 0.7648, 0.5691, 0.6819, 0.8134, 'mraky', 0.7281, 0.6799, 0.8590, 0.8786, 'mraky', 0.8475, 0.7056]}


# Data set will be corrected. The string values will changed to None values.
def replace_string_with_None(data, value_to_find, value_for_replacing):
    for key, value in data.items():
        data[key] = [value_for_replacing if i == value_to_find else i for i in value]
    return data
data = replace_string_with_None(data, "mraky", None)
print(data)


# Pandas DataFrame
frame = pd.DataFrame(data)
print(frame)
# Date string altered to Datetime
frame['Date'] =  pd.to_datetime(frame['Date'], format='%d-%b', errors='ignore')
frame['Date'] = frame['Date'].apply(lambda x: x.strftime('%d-%b'))
print(frame)

# Seaborn lineplot style and aesthetics.
sns.set_style('whitegrid')
sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.set_context("talk", rc={"lines.linewidth": 3})


num = 0
for column in frame.drop('Date', axis=1):
        num += 1
        result = sns.lineplot(x = 'Date', y = frame[column], data = frame, marker='o', palette = flatui)
result.set_xlabel('Date of acquisition', fontsize= 20)
result.set_ylabel('NDVI value', fontsize= 20)
result.set_title('Time series of NDVI', fontsize = 22)
plt.xticks(rotation = 45)
plt.show()
plt.savefig('myfile.pdf')

谢谢!

1 个答案:

答案 0 :(得分:0)

您将数据中的“ mraky”实例更改为“无”是导致此问题的原因。数据中的NaN值可能会对图表中处理点的顺序产生奇怪的影响。将它们更改为数字,就可以了。

更改

data = replace_string_with_None(data, "mraky", None)  

data = replace_string_with_None(data, "mraky", 0)