【问题标题】:How to get both Hlines and Vlines in the vizualisation如何在可视化中同时获得线条和线条
【发布时间】:2026-01-25 01:25:02
【问题描述】:

其中一行出现在可视化中

%matplotlib

import numpy as np
import matplotlib as mpl
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
def grid_sncf_generation_mitry():
    plt.figure(figsize=(50,100))


    # general var
    hcoord = np.arange(0,1,23)


    # Adding the time zone

    # J'ai restreint la plage horaire

    v2=np.datetime64('2019-09-13T03:30')
    v1=np.datetime64('2019-09-13T05:40')

    # Adding Stations V5 / V3 / SAS5 / SAS3

    plt.hlines("GA",v1,v2,"grey","dotted")
    plt.hlines("SAS3",v1,v2,"grey","dotted")
    plt.hlines("SAS5",v1,v2,"grey","dotted")
    plt.hlines("V3",v1,v2,"grey","dotted")
    plt.hlines("V5",v1,v2,"grey","dotted")

    #plt.vlines(hcoord,0,4)

    # id station
    plt.ylabel("MITRY")

    # Time axis
    # Adding Hours
    myFmt = mdates.DateFormatter('%H')
    plt.gca().xaxis.set_major_formatter(myFmt)
    plt.gca().xaxis.set_major_locator(mdates.HourLocator())

    # Adding Minutes
    myFmt2 = mdates.DateFormatter('%M')
    plt.gca().xaxis.set_minor_formatter(myFmt2)
    plt.gca().xaxis.set_minor_locator(mdates.MinuteLocator([10, 20, 30, 40, 50]))


    # Minimisize the police of minutes
    for tick in plt.gca().xaxis.get_minor_ticks():
        tick.label.set_fontsize(6)

    # comment détecter les heurs ?



    # comment détecter les 30's ?

    plt.show()
    return plt 

我想在可视化中同时获得 hlines 和 vlines

【问题讨论】:

  • 您的代码没有对plt.vlines 的任何未注释调用。具体来说,当您尝试这样做时出了什么问题?
  • 问题是当我从 plt.vlines(hcoord,0,4) 中删除 # 时,我只得到一条垂直线而水平线消失。

标签: python pandas numpy matplotlib data-visualization


【解决方案1】:

您的问题来自混合日期时间单位和非日期时间值。

当您在取消注释 plt.vlines(hcoord,0,4) 时运行代码时,您应该会收到一个异常:

fig = plt.figure()
hcoord = np.arange(0,1,23)
v2=np.datetime64('2019-09-13T03:30')
v1=np.datetime64('2019-09-13T05:40')

# Adding Stations V5 / V3 / SAS5 / SAS3

plt.hlines("GA",v1,v2,"grey","dotted")
plt.hlines("SAS3",v1,v2,"grey","dotted")
plt.hlines("SAS5",v1,v2,"grey","dotted")
plt.hlines("V3",v1,v2,"grey","dotted")
plt.hlines("V5",v1,v2,"grey","dotted")

plt.vlines(hcoord,0,4)
plt.show()

ValueError: 视图限制最小值 -36865.76180555556 小于 1 并且 是一个无效的 Matplotlib 日期值。如果您通过 具有日期时间单位的轴的非日期时间值

虽然用日期时间值替换 hcoord 可以按预期工作:

fig = plt.figure()
hcoord = np.arange(0,1,23)
v2=np.datetime64('2019-09-13T03:30')
v1=np.datetime64('2019-09-13T05:40')

# Adding Stations V5 / V3 / SAS5 / SAS3

plt.hlines("GA",v1,v2,"grey","dotted")
plt.hlines("SAS3",v1,v2,"grey","dotted")
plt.hlines("SAS5",v1,v2,"grey","dotted")
plt.hlines("V3",v1,v2,"grey","dotted")
plt.hlines("V5",v1,v2,"grey","dotted")

plt.vlines(v2,0,4)
plt.show()

不幸的是,我不明白你想用hccord = np.arange(0,1,23) 绘制什么,因为这条指令返回[0]。您必须弄清楚如何以日期时间为单位生成正确的 hcoord 数组,以获得您期望的绘图。

【讨论】:

  • 迪齐特,你就是那个男人! ty :D 你修好了我想做的。我不知道 hcoord 将什么作为值。
  • 如果您的问题得到解决,请考虑通过单击左侧的复选标记关闭主题来接受它