“ ValueError:年份超出范围”尝试使用matplotlib pyplot时 [英] "ValueError: year is out of range" when attempting to use matplotlib pyplot

查看:413
本文介绍了“ ValueError:年份超出范围”尝试使用matplotlib pyplot时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取matplotlib绘图函数,以便能够生成将x轴设置为时间轴的图形。但是,当我尝试根据UNIX时间绘制一些值时,遇到错误 ValueError:year超出范围。发生了什么问题以及如何解决?

I am attempting to get a matplotlib plotting function to be able to produce a graph with the x-axis set as a time axis. However, when I attempt to plot some values against UNIX times, I encounter the error ValueError: year is out of range. What is going wrong and how can it be addressed?

import os
import time

import matplotlib.dates
import matplotlib.pyplot
import shijian

def main():

    data = [
        [1484611200.0, 844.4333],
        [1484524800.0, 783.3373],
        [1484438400.0, 774.194 ],
        [1484352000.0, 769.2299]
    ]

    save_graph_matplotlib(
        values       = data,
        line         = True,
        line_width   = 0.5,
        title_axis_x = "time",
        title_axis_y = "value",
        #time_axis_x = True
    )

def save_graph_matplotlib(
    values              = None,
    title               = None,
    title_axis_x        = None,
    title_axis_y        = None,
    filename            = None,
    directory           = ".",
    overwrite           = True,
    color               = "black",
    LaTeX               = False,
    markers             = True,
    marker_size         = 1,
    aspect              = None,
    line                = False,
    line_style          = "-",
    line_width          = 0.2,
    font_size           = 20,
    scientific_notation = False,
    time_axis_x         = False
    ):

    # 1D or 2D data
    if isinstance(values[0], list):
        x = [element[0] for element in values]
        y = [element[1] for element in values]
    else:
        x = range(0, len(values))
        y = values

    matplotlib.pyplot.ioff()
    if LaTeX is True:
        matplotlib.pyplot.rc("text", usetex = True)
        matplotlib.pyplot.rc("font", family = "serif")
    if filename is None:
        if title is None:
            filename = "graph.png"
        else:
            filename = shijian.propose_filename(
                filename  = title + ".png",
                overwrite = overwrite
            )
    else:
        filename = shijian.propose_filename(
            filename  = filename,
            overwrite = overwrite
        )

    figure = matplotlib.pyplot.figure()

    if title is not None:
        figure.suptitle(
            title,
            fontsize = font_size
        )
    if markers is True:
        matplotlib.pyplot.scatter(
            x,
            y,
            s          = marker_size,
            c          = color,
            edgecolors = "none",
        )
    if line is True:
        matplotlib.pyplot.plot(
            x,
            y,
            line_style,
            c          = color,
            linewidth  = line_width
        )

    # Turn on or off axes scientific notation.
    if scientific_notation is False:
        matplotlib.pyplot.gca().get_xaxis().\
            get_major_formatter().set_scientific(False)
        matplotlib.pyplot.gca().get_yaxis().\
            get_major_formatter().set_scientific(False)
    # Set axes titles.
    if title_axis_x is not None:
        matplotlib.pyplot.xlabel(title_axis_x, fontsize = font_size)
    if title_axis_y is not None:
        matplotlib.pyplot.ylabel(title_axis_y, fontsize = font_size)
    # Set axes font size.
    matplotlib.pyplot.xticks(fontsize = font_size)
    matplotlib.pyplot.yticks(fontsize = font_size)
    # Set or do not set axis x as time.
    if time_axis_x:
        time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
        matplotlib.pyplot.axes().xaxis_date()
        matplotlib.pyplot.axes().xaxis.set_major_formatter(time_formatter)
        matplotlib.pyplot.xticks(rotation = -90)
    # Set the aspect ratio.
    if aspect is None:
        matplotlib.pyplot.axes().set_aspect(
            1 / matplotlib.pyplot.axes().get_data_ratio()
        )
    else:
        matplotlib.pyplot.axes().set_aspect(aspect)

    if not os.path.exists(directory):
        os.makedirs(directory)

    matplotlib.pyplot.savefig(
        directory + "/" + filename,
        dpi = 700
    )
    matplotlib.pyplot.close()

if __name__ == "__main__":
    main()


推荐答案

您需要将类似时间戳的x数据转换为python datetime对象,然后可以在matplotlib中使用该对象,并通过 matplotlib.dates.DateFormatter

You need to convert your timestamp-like x data to a python datetime object, which can then be used in matplotlib and be understood by the matplotlib.dates.DateFormatter.

这可以使用 datetime.datetime.fromtimestamp()方法完成。

import datetime
import matplotlib.dates
import matplotlib.pyplot as plt

data = [
        [1484611200.0, 844.4333],
        [1484524800.0, 783.3373],
        [1484438400.0, 774.194 ],
        [1484352000.0, 769.2299]
    ]

x = [datetime.datetime.fromtimestamp(element[0]) for element in data]
y = [element[1] for element in data]

plt.plot( x,  y,  ls="-",  c= "b",  linewidth  = 2 )
plt.xlabel("Dates")

time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
plt.axes().xaxis.set_major_formatter(time_formatter)
plt.axes().xaxis_date() # this is not actually necessary

plt.show()

这篇关于“ ValueError:年份超出范围”尝试使用matplotlib pyplot时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆