如何在可视化中获取线条和线条

问题描述 投票:-2回答:1

其中一条线出现在可视化中

%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

python pandas numpy matplotlib data-visualization
1个回答
0
投票

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

在取消注释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日期值。如果将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况

用datetime值替换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()

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.