matplotlib:如何自定义 x 轴刻度间隔
matplotlib: How to customise x-axis tick interval
我在设置 x 轴刻度间隔时遇到一些问题。
我有一个日期['01.13', '01.14', '01.15', '01.16', '01.17', '01.18', '01.19', '01.20', '01.21', '01.22', '01.23', ' 01.24'、'01.25'、'01.26'、'01.27'、'01.28'、'01.29'、'01.30'、...'05.27']。
我想画一些像
确认(日期),怀疑(日期),痊愈(日期),死亡(日期)
但是xtick标签重叠太多了
Q1:modify区间(x轴刻度标签如01.13,01.23,02.02....)
Q2:Does如果把'date[]'转成‘datetime’的格式,步骤会更简单吗?如何转换?
x=[]
y1=[]
y2=[]
y3=[]
y4=[]
for day_item in china_day_list:
date = day_item['date'] #+ '.2020'
confirm = day_item['confirm']
suspect = day_item['suspect']
dead = day_item['dead']
heal = day_item['heal']
nowConfirm = day_item['nowConfirm']
nowSevere = day_item['nowSevere']
deadRate = day_item['deadRate']
healRate = day_item['healRate']
############plt
x.append(date)
y1.append(confirm)
y2.append(suspect)
y3.append(dead)
y4.append(heal)
plt.figure(figsize=(20, 8))
plt.title('line graph of disease with time', fontsize=16)
plt.plot(x, y1, 'r-', label='C')
plt.plot(x, y2, 'b-',label='S')
plt.plot(x, y3, 'y-',label='D')
plt.plot(x, y4, 'g-',label='H')
plt.gcf().autofmt_xdate() # rotate label
plt.legend() #
我觉得可以用下面的代码自己自定义X轴。 Tick formatters
至于Q2,能做到的话我觉得没必要
labels = ['01.13','01.23','02.02','02.28','03.29','04.30']
ax.xaxis.set_major_formatter(ticker.FixedFormatter(labels))
我在设置 x 轴刻度间隔时遇到一些问题。
我有一个日期['01.13', '01.14', '01.15', '01.16', '01.17', '01.18', '01.19', '01.20', '01.21', '01.22', '01.23', ' 01.24'、'01.25'、'01.26'、'01.27'、'01.28'、'01.29'、'01.30'、...'05.27']。
我想画一些像
确认(日期),怀疑(日期),痊愈(日期),死亡(日期)
但是xtick标签重叠太多了
Q1:modify区间(x轴刻度标签如01.13,01.23,02.02....)
Q2:Does如果把'date[]'转成‘datetime’的格式,步骤会更简单吗?如何转换?
x=[]
y1=[]
y2=[]
y3=[]
y4=[]
for day_item in china_day_list:
date = day_item['date'] #+ '.2020'
confirm = day_item['confirm']
suspect = day_item['suspect']
dead = day_item['dead']
heal = day_item['heal']
nowConfirm = day_item['nowConfirm']
nowSevere = day_item['nowSevere']
deadRate = day_item['deadRate']
healRate = day_item['healRate']
############plt
x.append(date)
y1.append(confirm)
y2.append(suspect)
y3.append(dead)
y4.append(heal)
plt.figure(figsize=(20, 8))
plt.title('line graph of disease with time', fontsize=16)
plt.plot(x, y1, 'r-', label='C')
plt.plot(x, y2, 'b-',label='S')
plt.plot(x, y3, 'y-',label='D')
plt.plot(x, y4, 'g-',label='H')
plt.gcf().autofmt_xdate() # rotate label
plt.legend() #
我觉得可以用下面的代码自己自定义X轴。 Tick formatters 至于Q2,能做到的话我觉得没必要
labels = ['01.13','01.23','02.02','02.28','03.29','04.30']
ax.xaxis.set_major_formatter(ticker.FixedFormatter(labels))