每个日期都太接近

发布于 2025-01-12 11:54:40 字数 1345 浏览 2 评论 0原文

我有一个简单的图表,显示了一段时间内的比特币价格,正如您在图片中看到的那样。 输入图片此处的描述

问题是 x 轴上的日期彼此太接近。问题是我不需要显示每一个日期。我可以以某种方式将它们限制为 6 个标签,而不是您现在在图像上看到的 25 个标签吗?或者有没有更好的方法来处理日期?

我的代码:

def plot_data_out(x: array, y: array):
    x = np.array(x,dtype=datetime)
    y = np.array(y,dtype=float)
    print(x)
    print(y)
    plt.plot(x, y)
    plt.show()

x数据:

['2022-03-04 01:00:00' '2022-03-04 02:00:00' '2022-03-04 03:00:00'
 '2022-03-04 04:00:00' '2022-03-04 05:00:00' '2022-03-04 06:00:00'
 '2022-03-04 07:00:00' '2022-03-04 08:00:00' '2022-03-04 09:00:00'
 '2022-03-04 10:00:00' '2022-03-04 11:00:00' '2022-03-04 12:00:00'
 '2022-03-04 13:00:00' '2022-03-04 14:00:00' '2022-03-04 15:00:00'
 '2022-03-04 16:00:00' '2022-03-04 17:00:00' '2022-03-04 18:00:00'
 '2022-03-04 19:00:00' '2022-03-04 20:00:00' '2022-03-04 21:00:00'
 '2022-03-04 22:00:00' '2022-03-04 23:00:00' '2022-03-05 00:00:00'
 '2022-03-05 01:00:00']

y数据

[42454.   41603.36 41510.58 41403.7  41449.93 41379.99 41374.45 41407.98
 41436.98 41590.   41522.29 41676.56 41662.06 41511.3  41315.75 41106.91
 40694.32 40754.41 40840.53 40605.88 39782.13 39469.36 39392.83 39004.73
 39148.65]

I have simple graph showing bitcoin price in some time as you can see on image.
enter image description here

Problem is that on x axis there are dates too close to each other. Thing is that I dont need to show every one date. Can I somehow limit them to lets say 6 labels instead of 25 as you can see on image right now. Or is there any better way how to handle dates?

My code:

def plot_data_out(x: array, y: array):
    x = np.array(x,dtype=datetime)
    y = np.array(y,dtype=float)
    print(x)
    print(y)
    plt.plot(x, y)
    plt.show()

x data:

['2022-03-04 01:00:00' '2022-03-04 02:00:00' '2022-03-04 03:00:00'
 '2022-03-04 04:00:00' '2022-03-04 05:00:00' '2022-03-04 06:00:00'
 '2022-03-04 07:00:00' '2022-03-04 08:00:00' '2022-03-04 09:00:00'
 '2022-03-04 10:00:00' '2022-03-04 11:00:00' '2022-03-04 12:00:00'
 '2022-03-04 13:00:00' '2022-03-04 14:00:00' '2022-03-04 15:00:00'
 '2022-03-04 16:00:00' '2022-03-04 17:00:00' '2022-03-04 18:00:00'
 '2022-03-04 19:00:00' '2022-03-04 20:00:00' '2022-03-04 21:00:00'
 '2022-03-04 22:00:00' '2022-03-04 23:00:00' '2022-03-05 00:00:00'
 '2022-03-05 01:00:00']

y data

[42454.   41603.36 41510.58 41403.7  41449.93 41379.99 41374.45 41407.98
 41436.98 41590.   41522.29 41676.56 41662.06 41511.3  41315.75 41106.91
 40694.32 40754.41 40840.53 40605.88 39782.13 39469.36 39392.83 39004.73
 39148.65]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2025-01-19 11:54:40

您可以将 x 轴数据转换为日期时间格式,然后用它来绘制图表。即使这样,您也可以看到标签看起来很拥挤。您可以将标签旋转 30 度并获得更清晰的图表

v = ['2022-03-04 01:00:00','2022-03-04 02:00:00','2022-03-04 03:00:00',
 '2022-03-04 04:00:00','2022-03-04 05:00:00','2022-03-04 06:00:00',
 '2022-03-04 07:00:00','2022-03-04 08:00:00','2022-03-04 09:00:00',
 '2022-03-04 10:00:00','2022-03-04 11:00:00','2022-03-04 12:00:00',
 '2022-03-04 13:00:00','2022-03-04 14:00:00','2022-03-04 15:00:00',
 '2022-03-04 16:00:00','2022-03-04 17:00:00','2022-03-04 18:00:00',
 '2022-03-04 19:00:00','2022-03-04 20:00:00','2022-03-04 21:00:00',
 '2022-03-04 22:00:00','2022-03-04 23:00:00','2022-03-05 00:00:00',
 '2022-03-05 01:00:00']

y = [42454.,41603.36,41510.58,41403.7 ,41449.93,41379.99,41374.45,41407.98,
 41436.98, 41590. ,  41522.29, 41676.56 ,41662.06, 41511.3  ,41315.75, 41106.91,
 40694.32, 40754.41 ,40840.53, 40605.88, 39782.13 ,39469.36 ,39392.83 ,39004.73,
 39148.65]

v1 = [datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in v]

import matplotlib.pyplot as plt
plt.plot(v1,y)
plt.xticks(rotation = 30)

You can convert the x axis data to datetime format and then use it to plot the graph. Even then you can see the labels look congested. You can rotate the labels by 30 degrees and have more legible chart

v = ['2022-03-04 01:00:00','2022-03-04 02:00:00','2022-03-04 03:00:00',
 '2022-03-04 04:00:00','2022-03-04 05:00:00','2022-03-04 06:00:00',
 '2022-03-04 07:00:00','2022-03-04 08:00:00','2022-03-04 09:00:00',
 '2022-03-04 10:00:00','2022-03-04 11:00:00','2022-03-04 12:00:00',
 '2022-03-04 13:00:00','2022-03-04 14:00:00','2022-03-04 15:00:00',
 '2022-03-04 16:00:00','2022-03-04 17:00:00','2022-03-04 18:00:00',
 '2022-03-04 19:00:00','2022-03-04 20:00:00','2022-03-04 21:00:00',
 '2022-03-04 22:00:00','2022-03-04 23:00:00','2022-03-05 00:00:00',
 '2022-03-05 01:00:00']

y = [42454.,41603.36,41510.58,41403.7 ,41449.93,41379.99,41374.45,41407.98,
 41436.98, 41590. ,  41522.29, 41676.56 ,41662.06, 41511.3  ,41315.75, 41106.91,
 40694.32, 40754.41 ,40840.53, 40605.88, 39782.13 ,39469.36 ,39392.83 ,39004.73,
 39148.65]

v1 = [datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in v]

import matplotlib.pyplot as plt
plt.plot(v1,y)
plt.xticks(rotation = 30)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文