如何连接符合条件的地块上的所有远点?
How to connect all distant points on a plot which follow a condition?
我正在根据 "Date Weekday" 绘制我的锻炼重量。我想连接所有符合条件的点:
weights = ([1471.5, 4026. , 2399.5, 5462. , 0. , 2758.1, 6229. , 5132. ])
dates = ['16 Thu', '20 Mon', '21 Tue', '22 Wed', '23 Thu', '24 Fri','25 Sat','27 Mon']
any(x in dates[i] for x in ["Mon","Thu"])
我看到了这个问题但是这解决了每两个问题,但我的问题是我可能会跳过几天。我希望代码健壮。
我在上面的 "dates" 和 "weights" 中使用了它:
plt.pyplot.plot(dates,weights,'ro-')
plt.pyplot.xticks(dates)
plt.pyplot.grid(b=True, which='major', color='#666666', linestyle='-')
plt.pyplot.yticks(weights)
plt.pyplot.show()
产生了这个不连贯的情节:
我想要这个输出:
所以我这样做了:
MonThudate = []
TueFridate = []
WedSatdate = []
MonThuwt = []
TueFriwt = []
WedSatwt = []
for i in range(len(dates)):
if any(x in dates[i] for x in ["Mon","Thu"]):
MonThudate.append(dates[i])
MonThuwt.append(weights[i])
if any(x in dates[i] for x in ["Tue","Fri"]):
TueFridate.append(dates[i])
TueFriwt.append(weights[i])
if any(x in dates[i] for x in ["Wed","Sat"]):
WedSatdate.append(dates[i])
WedSatwt.append(weights[i])
#MonThuDate = ['16 Thu', '20 Mon', '23 Thu', '27 Mon']
#MonThuwt = [1471.5, 4026.0, 0.0, 5132.0]
plt.pyplot.plot(MonThudate,MonThuwt,'ro-')
plt.pyplot.plot(WedSatdate,WedSatwt,'ro-')
plt.pyplot.plot(TueFridate,TueFriwt,'ro-')
它产生了这个:
但是在这里,整个图表的 x 轴并不相同。它是不相交的。
所以基本上您只想连接一周中的特定几天。然后你需要用正确的组标签标记这些日期并在每个组上绘制:
# create day group:
day_groups = [['Thu','Mon'], ['Tue','Fri'], ['Wed', 'Sat'],['Sun']]
day_dict = {day:i for i,groups in enumerate(day_groups) for day in groups}
# {'Thu': 0, 'Mon': 0, 'Tue': 1, 'Fri': 1, 'Wed': 2, 'Sat': 2, 'Sun': 3}
# label the days in the given data set:
# if df.dates is datetime, we can do df.dates.dt.day_name().str[:3]
day_labels = df.dates.str.split(expand=True)[1].map(day_dict)
# groupby and plot:
fig, ax = plt.subplots(figsize=(10,6))
df.weights.groupby(day_labels).plot(ax=ax, marker='o')
ax.set_xticklabels([0] + df.dates.tolist())
ax.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()
输出:
注意:建议您将df.dates
转换为datetime
以自动排列x-axis。
我正在根据 "Date Weekday" 绘制我的锻炼重量。我想连接所有符合条件的点:
weights = ([1471.5, 4026. , 2399.5, 5462. , 0. , 2758.1, 6229. , 5132. ])
dates = ['16 Thu', '20 Mon', '21 Tue', '22 Wed', '23 Thu', '24 Fri','25 Sat','27 Mon']
any(x in dates[i] for x in ["Mon","Thu"])
我看到了这个问题
我在上面的 "dates" 和 "weights" 中使用了它:
plt.pyplot.plot(dates,weights,'ro-')
plt.pyplot.xticks(dates)
plt.pyplot.grid(b=True, which='major', color='#666666', linestyle='-')
plt.pyplot.yticks(weights)
plt.pyplot.show()
产生了这个不连贯的情节:
我想要这个输出:
所以我这样做了:
MonThudate = []
TueFridate = []
WedSatdate = []
MonThuwt = []
TueFriwt = []
WedSatwt = []
for i in range(len(dates)):
if any(x in dates[i] for x in ["Mon","Thu"]):
MonThudate.append(dates[i])
MonThuwt.append(weights[i])
if any(x in dates[i] for x in ["Tue","Fri"]):
TueFridate.append(dates[i])
TueFriwt.append(weights[i])
if any(x in dates[i] for x in ["Wed","Sat"]):
WedSatdate.append(dates[i])
WedSatwt.append(weights[i])
#MonThuDate = ['16 Thu', '20 Mon', '23 Thu', '27 Mon']
#MonThuwt = [1471.5, 4026.0, 0.0, 5132.0]
plt.pyplot.plot(MonThudate,MonThuwt,'ro-')
plt.pyplot.plot(WedSatdate,WedSatwt,'ro-')
plt.pyplot.plot(TueFridate,TueFriwt,'ro-')
它产生了这个:
但是在这里,整个图表的 x 轴并不相同。它是不相交的。
所以基本上您只想连接一周中的特定几天。然后你需要用正确的组标签标记这些日期并在每个组上绘制:
# create day group:
day_groups = [['Thu','Mon'], ['Tue','Fri'], ['Wed', 'Sat'],['Sun']]
day_dict = {day:i for i,groups in enumerate(day_groups) for day in groups}
# {'Thu': 0, 'Mon': 0, 'Tue': 1, 'Fri': 1, 'Wed': 2, 'Sat': 2, 'Sun': 3}
# label the days in the given data set:
# if df.dates is datetime, we can do df.dates.dt.day_name().str[:3]
day_labels = df.dates.str.split(expand=True)[1].map(day_dict)
# groupby and plot:
fig, ax = plt.subplots(figsize=(10,6))
df.weights.groupby(day_labels).plot(ax=ax, marker='o')
ax.set_xticklabels([0] + df.dates.tolist())
ax.grid(b=True, which='major', color='#666666', linestyle='-')
plt.show()
输出:
注意:建议您将df.dates
转换为datetime
以自动排列x-axis。