我想(在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来完成,它只是我最熟悉的绘图包。
发布于 2020-01-10 16:35:55
上使用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()

https://stackoverflow.com/questions/59685022
复制相似问题