繁体   English   中英

Matplotlib 多行 plot 按日期着色

[英]Matplotlib multi-line plot coloured by date

我有一个简单的 XY plot 一些数据(变量 A 与变量 B),其中每条上划线显示不同时间段的数据的数据。 我希望能够根据日期为颜色图中的每一行(系列)着色(我实际上想要按月着色),而不仅仅是添加数据的顺序。 关于如何轻松做到这一点的任何想法? 我阅读的所有内容似乎都准备根据循环迭代器而不是数据本身来选择颜色。

我的数据看起来像这样(这只是一个片段):

DATE (YYYY-MM-DDTHH:MI:SSZ) A       B
2019-11-28T13:39:00Z        8.4     5.753
2019-11-28T13:39:00Z        10.5    5.755
2019-11-28T13:39:00Z        12.4    5.753
2019-11-28T13:39:00Z        14.5    5.753
2019-11-28T13:39:00Z        16.7    5.753
2019-11-28T13:39:00Z        18.5    5.752
2019-11-28T13:39:00Z        20.2    5.75
2019-11-30T13:59:30Z        9.1     6.167
2019-11-30T13:59:30Z        10.2    6.165
2019-11-30T13:59:30Z        10.9    6.167
2019-11-30T13:59:30Z        11.8    6.166
2019-11-30T13:59:30Z        12.9    6.166
2019-11-30T13:59:30Z        13.8    6.168
2019-11-30T13:59:30Z        14.9    6.166
2019-11-30T13:59:30Z        15.9    6.165
2019-11-30T13:59:30Z        17      6.166
2019-11-30T13:59:30Z        17.9    6.166
2019-11-30T13:59:30Z        18.9    6.166
2019-11-30T13:59:30Z        20      6.168
2019-11-30T13:59:30Z        1.8     6.159
2019-11-30T13:59:30Z        2.8     6.16
2019-11-30T13:59:30Z        4       6.161
2019-11-30T13:59:30Z        5.1     6.161
2019-11-30T13:59:30Z        6.1     6.161
2019-11-30T13:59:30Z        6.9     6.165
2019-11-30T13:59:30Z        8.1     6.168
2019-12-03T13:34:30Z        3.2     5.716
2019-12-03T13:34:30Z        3.8     5.715
2019-12-03T13:34:30Z        4.8     5.714
2019-12-03T13:34:30Z        5.9     5.715
2019-12-03T13:34:30Z        7.1     5.714
2019-12-03T13:34:30Z        8.1     5.715
2019-12-03T13:34:30Z        8.8     5.722
2019-12-03T13:34:30Z        9.8     5.722
2019-12-03T13:34:30Z        10.9    5.721
2019-12-03T13:34:30Z        11.9    5.722
2019-12-03T13:34:30Z        12.9    5.722
2019-12-03T13:34:30Z        14      5.726
2019-12-03T13:34:30Z        15.3    5.728
2019-12-03T13:34:30Z        16.1    5.727
2019-12-03T13:34:30Z        16.9    5.727
2019-12-03T13:34:30Z        17.8    5.726
2019-12-03T13:34:30Z        18.9    5.728
2019-12-03T13:34:30Z        20      5.728

这是一个“丑陋”的尝试,您可能会在 Stack Overflow 帖子“Matplotlib:每个日期的不同 colors,通过颜色条标记”和“将日期的 Map 上色为 Python 中的字符串”中发现有用的尝试:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

from datetime import datetime
from mpl_toolkits.axes_grid1 import make_axes_locatable

if __name__ == "__main__":

    df = pd.read_csv("data.txt", delim_whitespace=True)

    # Change format time
    df['DATE SIMPLE'] = pd.to_datetime(df['DATE(YYYY-MM-DDTHH:MI:SSZ)']).dt.strftime('%d-%m-%Y')

    # Add colormap
    cmap = plt.get_cmap("viridis")
    colors = [cmap(int(i)) for i in np.linspace(0, cmap.N, len(df))]

    # Start figure
    fig, ax = plt.subplots()

    # Format time for ticks colorbar
    loc = mdates.AutoDateLocator(minticks=5, maxticks=7)
    plt_time = [mdates.date2num(datetime.strptime(timestamp, "%d-%m-%Y")) for timestamp in df['DATE SIMPLE']]

    # Plot XY using matplotlib time for color (plt_time) so the colorbar understands the ticks later
    sc = ax.scatter(x=df["A"], y=df["B"], c=plt_time)
    ax.set_xlabel("A")
    ax.set_ylabel("B")

    # Add colorbar to graph
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("bottom", size="5%", pad=0.50)
    cbar = fig.colorbar(sc, cax=cax, orientation='horizontal', ticks=loc,
                        format=mdates.DateFormatter("%d-%m-%Y"), label="Date (Day-Month-Year)")

    # The ugly part: 'Recolor' the scatter plot with the colors we calculated earlier
    ax.scatter(x=df["A"], y=df["B"], c=colors)
    plt.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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