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

Python中日期作为字符串的颜色映射,python,matplotlib,colormap,Python,Matplotlib,Colormap,我想(用python)绘制我在x轴上跑的距离和我在y轴上跑这个距离的速度。对于每个数据点,我都有一个字符串,它表示运行发生的日期。现在我想做一个散点图,其中数据点的颜色表示它发生的日期。此外,彩色地图应合理标记为日期或年份,而不是数字。 数据如下所示: x = [2.803480599999999, 5.5502475000000056, 6.984381300000002, 4.115224099999998, 5.746583699999995, 8.971469500000019, 12.

我想(用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']
t_num = [0, 6, 34, 40, 41, 47, 48, 51, 55, 58, 62, 75, 77, 79, 81, 87, 90, 94]
我试图做的是使用Unix格式将
t\u字符串
转换为一个数字,然后将其转换为从第0天开始的一天。结果如下所示:

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_num = [0, 6, 34, 40, 41, 47, 48, 51, 55, 58, 62, 75, 77, 79, 81, 87, 90, 94]
然后,我使用matlabplotlib绘制了数据,但正如您所见,colormap的标签并没有真正意义

import matplotlib.pyplot as plt

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

如果我尝试使用
t\u string
而不是
t\u 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'].
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/axes/_axes.py”,第4284行,在_parse_scatter_color_args中
颜色=mcolors.to_rgba_数组(c)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/colors.py”,第294行,在to_rgba_数组中
结果[i]=至_rgba(cc,alpha)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/colors.py”,第177行,to_rgba
rgba=_至_rgba_无颜色循环(c,alpha)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/colors.py”,第233行,在_to_rgba_no_colorcycle中
raise VALUERROR(“无效的RGBA参数:{!r}.”格式(orig_c))
ValueError:无效的RGBA参数:“2019-10-7”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Volumes/JOEL-USB/AnalyseMyRun 0.2/scripts/main.py”,第16行,在
按距离绘制分布图(2)
文件“/Volumes/JOEL USB/AnalyseMyRun 0.2/scripts/data\u plot\u types.py”,第133行,按距离分布
plt.散射(列表距离到排序、列表速度平均排序、,
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/pyplot.py”,第2836行,分散显示
__ret=gca()。散布(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/__init__.py”,第1599行,内部
return func(ax,*map(sanitize_序列,args),**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/axes/_axes.py”,第4451行,分散显示
self.\u解析\u分散\u颜色\u参数(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/matplotlib/axes/_axes.py”,第4302行,在_parse_scatter_color_args中
升值误差(
ValueError:“c”参数必须是mpl颜色、mpl颜色序列或数字序列,而不是['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-21','2019-12-23','2019-12-25','2019-12-27','2020-1-2','2020-1-9']。
有人有办法解决这个问题吗?图形不需要在matplotlib中完成,它只是我最熟悉的绘图包

  • 将日期字符串转换为matplotlib内部日期格式
  • 在颜色栏上使用
    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()