Python 中作为字符串的日期颜色图

Color Map of Date as String in Python

我想绘制(在 python 中)我在 x 轴上 运行 的距离和我在 y 轴上 运行 这个距离的速度。对于每个数据点,我都有一个字符串,表示 运行 发生的日期。现在我想制作一个散点图,其中数据点的颜色代表它发生的日期。此外,颜色图应该被合理地标记为日期或年份,而不是数字。 数据看起来像这样:

x = [2.803480599999999, 5.5502475000000056, 6.984381300000002, 4.115224099999998, 5.746583699999995, 8.971469500000019, 12.028179500000032, 13.451193300000014, 12.457393999999972, 12.027555199999998, 16.077930800000015, 5.021229700000006, 11.206380399999999, 7.903262600000004, 11.98195070000001, 12.21701, 10.35045, 10.231890000000002]

y = [11.961321698938578, 5.218986480632915, 5.211628408660906, 4.847852635777481, 4.936266162218553, 5.233256380128127, 5.441388698929861, 5.461721129728066, 5.722170570613203, 5.2698434785261545, 5.645419662253215, 4.617062894639794, 4.973357261130752, 5.906843248930297, 5.256517482861392, 5.537361432952908, 5.339542403148332, 5.376979880224148]

t_string = ['2019-10-7', '2019-10-13', '2019-11-10', '2019-11-16', '2019-11-17', '2019-11-23', '2019-11-24', '2019-11-27', '2019-12-1', '2019-12-4', '2019-12-8', '2019-12-21', '2019-12-23', '2019-12-25', '2019-12-27', '2020-1-2', '2020-1-5', '2020-1-9']

我尝试做的是使用 Unix 格式将 t_string 转换为数字,然后将其转换为从第 0 天开始的一天。结果如下所示:

t_num = [0, 6, 34, 40, 41, 47, 48, 51, 55, 58, 62, 75, 77, 79, 81, 87, 90, 94]

然后我使用 matlabplotlib 绘制了数据,但如您所见,颜色图的标签并没有真正意义...

import matplotlib.pyplot as plt

plt.scatter(x,y,
            c=t_num,
            cmap='viridis')
plt.colorbar()

如果我尝试使用 t_string 而不是 t_num,我会收到以下错误消息

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 4284, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/colors.py", line 294, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/colors.py", line 177, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/colors.py", line 233, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: '2019-10-7'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Volumes/JOEL USB/AnalyseMyRun 0.2/scripts/main.py", line 16, in <module>
    plot_distributionby_distance(2)
  File "/Volumes/JOEL USB/AnalyseMyRun 0.2/scripts/data_plot_types.py", line 133, in plot_distributionby_distance
    plt.scatter(list_DistanceTot_sorted,list_PaceAverage_sorted,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2836, in scatter
    __ret = gca().scatter(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/__init__.py", line 1599, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 4451, in scatter
    self._parse_scatter_color_args(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 4302, in _parse_scatter_color_args
    raise ValueError(
ValueError: 'c' argument must be a mpl color, a sequence of mpl colors or a sequence of numbers, not ['2019-10-7', '2019-10-13', '2019-11-10', '2019-11-16', '2019-11-17', '2019-11-23', '2019-11-24', '2019-11-27', '2019-12-1', '2019-12-4', '2019-12-8', '2019-12-21', '2019-12-23', '2019-12-25', '2019-12-27', '2020-1-2', '2020-1-5', '2020-1-9'].

有人有办法解决这个问题吗?作图不用matplotlib,就是我最熟悉的作图包。

  1. 将您的日期字符串转换为 matplotlib 内部日期格式
  2. 在颜色条上使用 matplotlib.dates' 定位符和格式化程序

示例:

import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates

x = [2.803480599999999, 5.5502475000000056, 6.984381300000002, 4.115224099999998, 5.746583699999995, 8.971469500000019, 12.028179500000032, 13.451193300000014, 12.457393999999972, 12.027555199999998, 16.077930800000015, 5.021229700000006, 11.206380399999999, 7.903262600000004, 11.98195070000001, 12.21701, 10.35045, 10.231890000000002]

y = [11.961321698938578, 5.218986480632915, 5.211628408660906, 4.847852635777481, 4.936266162218553, 5.233256380128127, 5.441388698929861, 5.461721129728066, 5.722170570613203, 5.2698434785261545, 5.645419662253215, 4.617062894639794, 4.973357261130752, 5.906843248930297, 5.256517482861392, 5.537361432952908, 5.339542403148332, 5.376979880224148]

t_string = ['2019-10-7', '2019-10-13', '2019-11-10', '2019-11-16', '2019-11-17', '2019-11-23', '2019-11-24', '2019-11-27', '2019-12-1', '2019-12-4', '2019-12-8', '2019-12-21', '2019-12-23', '2019-12-25', '2019-12-27', '2020-1-2', '2020-1-5', '2020-1-9']
t = [mdates.date2num(datetime.strptime(i, "%Y-%m-%d")) for i in t_string]

fig, ax = plt.subplots()
sc = ax.scatter(x,y, c=t)

loc = mdates.AutoDateLocator()
fig.colorbar(sc, ticks=loc,
                 format=mdates.AutoDateFormatter(loc))

plt.show()