Python:如何使用matplotlib.pyplot更改x轴间隔以显示12个月?
我试图制作一张图表,显示一整年的数据。数据来自CSV文件(records.CSV),如下所示:Python:如何使用matplotlib.pyplot更改x轴间隔以显示12个月?,python,matplotlib,Python,Matplotlib,我试图制作一张图表,显示一整年的数据。数据来自CSV文件(records.CSV),如下所示: month,data 2016-01,66 2016-02,68 2016-03,70 2016-04,72 2016-05,74 2016-06,76 2016-07,78 2016-08,80 2016-09,82 2016-10,84 2016-11,86 2016-12,88 import csv from datetime import datetime from matplotlib i
month,data
2016-01,66
2016-02,68
2016-03,70
2016-04,72
2016-05,74
2016-06,76
2016-07,78
2016-08,80
2016-09,82
2016-10,84
2016-11,86
2016-12,88
import csv
from datetime import datetime
from matplotlib import pyplot as plt
def date_to_list(index):
""" save date to a list """
results = []
for row in data:
results.append(datetime.strptime(row[index], '%Y-%m'))
return results
def data_to_list(index):
""" save data to a list """
results = []
for row in data:
results.append(int(row[index]))
return results
filename = 'records.csv'
with open(filename) as f:
data = csv.reader(f)
header = next(data)
data = list(data)
# save data and date to list
records = data_to_list(1)
date = date_to_list(0)
plt.plot(date, records)
plt.show()
我的代码如下:
month,data
2016-01,66
2016-02,68
2016-03,70
2016-04,72
2016-05,74
2016-06,76
2016-07,78
2016-08,80
2016-09,82
2016-10,84
2016-11,86
2016-12,88
import csv
from datetime import datetime
from matplotlib import pyplot as plt
def date_to_list(index):
""" save date to a list """
results = []
for row in data:
results.append(datetime.strptime(row[index], '%Y-%m'))
return results
def data_to_list(index):
""" save data to a list """
results = []
for row in data:
results.append(int(row[index]))
return results
filename = 'records.csv'
with open(filename) as f:
data = csv.reader(f)
header = next(data)
data = list(data)
# save data and date to list
records = data_to_list(1)
date = date_to_list(0)
plt.plot(date, records)
plt.show()
然后我得到了如下图表:
目前,我正努力解决以下两个问题:
您可以使用
labels=[i[0]为数据中的i]
设置自定义标签,并使用
xvalues = np.arange(0, len(labels), 1)
plt.xticks(xvalues, labels)
plt.xlim(-0.5, len(labels) - 0.5)
这将按照您在问题中使用的样式为每个月生成一个标签。选择x限制时,应确保记号居中。但是,请注意,x轴有点混乱。为了避免这种情况,您可以:
要设置记号的位置,可以使用
matplotlib.dates.MonthLocator
。为了使记号具有特定的格式,可以使用matplotlib.dates.DateFormatter
。为了避免标签重叠,可以使用autofmt_xdate()
。为了没有边距,您可以使用plt.margins(x=0,y=0)
:
完整代码:
data = u"""month,data
2016-01,66
2016-02,68
2016-03,70
2016-04,72
2016-05,74
2016-06,76
2016-07,78
2016-08,80
2016-09,82
2016-10,84
2016-11,86
2016-12,88"""
import matplotlib.pyplot as plt
import matplotlib.ticker
import matplotlib.dates
import csv
from datetime import datetime
import io
def date_to_list(index):
""" save date to a list """
results = []
for row in data:
results.append(datetime.strptime(row[index], '%Y-%m'))
return results
def data_to_list(index):
""" save data to a list """
results = []
for row in data:
results.append(int(row[index]))
return results
with io.StringIO(data) as f:
data = csv.reader(f)
header = next(data)
data = list(data)
# save data and date to list
records = data_to_list(1)
date = date_to_list(0)
plt.plot(date, records)
plt.gca().xaxis.set_major_locator(matplotlib.dates.MonthLocator())
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y-%m"))
plt.gcf().autofmt_xdate()
plt.margins(x=0,y=0)
plt.show()
这条线周围仍有一些余量。不要弄乱轴的最好方法是旋转标签。