Python 如何添加星期x轴标签

Python 如何添加星期x轴标签,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个数据帧,其中每一行都与一周中的一天相关,从周日开始。总共有39行。我想绘制列Physical中的数值,一次一周,在x轴上标记一周中的几天。到目前为止,我有: import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv("health.csv", header = None, names = ['Physical', 'Emotional']) ax = df['Physical'].plot() plt.sh

我有一个数据帧,其中每一行都与一周中的一天相关,从周日开始。总共有39行。我想绘制列
Physical
中的数值,一次一周,在x轴上标记一周中的几天。到目前为止,我有:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("health.csv", header = None, names = ['Physical', 'Emotional'])
ax = df['Physical'].plot()
plt.show()
这使得:

如何以一周中的几天为x轴添加标签,从星期日开始并拆分周,以便它们垂直分开

全部数据如下:

5,5
6,7
6,9
6,7
5,6
7,9
5,9
6,7
7,6
7,4
7,5
6,7
7,9
7,9
5,6
8,7
9,9
7,7
7,6
7,8
7,9
7,9
7,6
7,8
6,6
6,6
6,7
6,6
6,5
6,6
7,5
7,5
7,5
7,6
7,5
8,6
7,6
7,7
6,6
像这样的

import pandas as pd
import calendar

df = pd.DataFrame({'Physical': {0: 5,
  1: 6,2: 6,3: 6,4: 5,5: 7,6: 5,7: 6,8: 7,9: 7,10: 7,11: 6,12: 7,
  13: 7,14: 5,15: 8,16: 9,17: 7,18: 7,19: 7,20: 7,21: 7,22: 7,23: 7,24: 6,25: 6,
  26: 6,27: 6,28: 6,29: 6,30: 7,31: 7,32: 7,33: 7,34: 7,35: 8,36: 7,37: 7,38: 6}})

# Get Dayofweek index number (start with 6 for sunday) 6,0,1....
df['DayOfTheWeek'] = [(i+6) % 7  for i in range(len(df))]

# Get a map to translate to day of week
d = dict(zip(range(7),list(calendar.day_name)))
df['DayOfTheWeek'] = df['DayOfTheWeek'].map(d)

# Loop through the df (splitting week by week)
for i in range(round(len(df)/7)):
    df.iloc[i*7:(i+1)*7].set_index('DayOfTheWeek').plot(kind='bar')

plt.show()
返回6个图像,这是一个:


您是第一个在这里询问如何在matplotlib绘图上绘制一周中的几天的人吗?其他答案在多大程度上没有帮助?如果您的数据没有日期列,您如何按天绘制ᴏʟᴅsᴘᴇᴇᴅ 事实上,他只是提到了一周中的某一天(所以我们可以选择任何日期,我想从周日开始):)。或者只分配Sun-Sat.@cᴏʟᴅsᴘᴇᴇᴅ 第一排是星期天,然后是星期一,诸如此类。我认为这被错误地标记为重复,因为您要求的是其他内容。希望我的回答能对你有所帮助!:)为什么这会被否决?@eleanora我不知道。没关系,对你有帮助吗?谢谢。我想你需要
范围内的I(int(round(len(df)/7)):
因为round返回一个float。它不会绘制最后几天,因为它似乎只覆盖整个星期。另外,如何使每个图形的y轴范围相同?我尝试在循环中添加
plt.ylim([0,10])
,但效果不好。@eleanora我们可以使用子批来实现这一点。使用matplotlib,您始终需要设置很多格式:/