Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何使用matplotlib.pyplot更改x轴间隔以显示12个月?_Python_Matplotlib - Fatal编程技术网

Python:如何使用matplotlib.pyplot更改x轴间隔以显示12个月?

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

我试图制作一张图表,显示一整年的数据。数据来自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 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()
然后我得到了如下图表:

目前,我正努力解决以下两个问题:

  • 这里的x轴只显示6个月,我怎么能得到12个月?或者 另一种说法是,如何根据需要更改间隔,例如 月/季/年
  • 我想在这条线的左右两边都有一些空间 这也是由于x轴的范围。我怎样才能直接排队 从矩形区域的左侧(右侧)开始(&end)

  • 您可以使用
    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轴有点混乱。为了避免这种情况,您可以:

  • 沿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()
    

    这条线周围仍有一些余量。不要弄乱轴的最好方法是旋转标签。