如何在用图进行子绘图时隐藏x轴值

时间:2019-05-27 21:28:24

标签: python pandas plotly

我正在使用plotly绘制一个较大的csv文件(包含大量数据)并在子图中按行绘制(实际上有效)一些图表。 问题在于,对于每个图表,我都获得了x轴上每个点的所有日期和时间,从而导致质量不好的图表。如何隐藏前五个图表的x轴值,同时保留最后一个图表的x轴,以更好地查看图表?

这是我的代码:有问题的部分从第125行开始

import os
import pandas as pd
import plotly
import plotly.graph_objs as go
from tkinter import *


window = Tk()
window.title("Interface utilisateur")

consigne=Label(window, text="Merci de remplir les champs ci-dessous avant de cliquer sur start")
consigne.grid(column = 1,row=0)

consDateDEB=Label(window, text="Date de début de sélection (format JJMMAAAA)")
consDateDEB.grid(column=0,row=1)
DateDEB = Entry(window,width=25)
DateDEB.grid(column = 2,row=1)

consDateFIN=Label(window, text="Date de fin de sélection (format JJMMAAAA)")
consDateFIN.grid(column=0,row=2)
DateFIN = Entry(window,width=25)
DateFIN.grid(column = 2,row=2)

consPathIN=Label(window, text="Chemin d'accès aux fichiers (format C://user/dossier1/dossier2/)")
consPathIN.grid(column=0,row=3)
PathIN = Entry(window,width=50)
PathIN.grid(column = 2,row=3)

consPathOUT=Label(window, text="Chemin d'écriture des fichiers (format C://user/dossier1/dossier2/)")
consPathOUT.grid(column=0,row=4)
PathOUT = Entry(window,width=50)
PathOUT.grid(column = 2,row=4)

def click():
        pathIN = PathIN.get()
        pathOUT = PathOUT.get()
        DateDebut = DateDEB.get()
        DateFin = DateFIN.get()
        tracer(DateDebut,DateFin,pathIN,pathOUT)
btn = Button(window, text='Start',command=click,width=30,height=2,activebackground='red')
btn.grid(column = 1,row=5)

window.mainloop() 

def tracer(DateDebut,DateFin,pathIN,pathOUT):
#    DateDebut=10052019
#    DateFin=12052019
#    pathIN='D://Clef64go/PJT/Logfiles/'
#    pathOUT='D://Clef64go/PJT/OUT/'
    Logfiles = os.listdir(pathIN)

    def conversion(Logfile_JJMMAAAA):
        nomFichierINT = int(Logfile_JJMMAAAA[12:16] + Logfile_JJMMAAAA[10:12] + Logfile_JJMMAAAA[8:10])
        return nomFichierINT

    def conversionInverse(AAAAMMJJ):
        AAAAMMJJ = str(AAAAMMJJ) 
        nomFichierSTR = "Logfile_" + AAAAMMJJ[6:8] + AAAAMMJJ[4:6] + AAAAMMJJ[0:4]+".csv"
        return nomFichierSTR

    DateDebut = str(DateDebut)
    DateFin = str(DateFin)
    DebTempo = DateDebut[4:8]+DateDebut[2:4]+DateDebut[0:2]
    FinTempo = DateFin[4:8]+DateFin[2:4]+DateFin[0:2]
    DateDebut=int(DebTempo)
    DateFin=int(FinTempo)

    L_Selection=[]
    for fichier in Logfiles:
        Tempo=conversion(fichier)
        if Tempo >= DateDebut and Tempo <= DateFin :
            L_Selection.append(Tempo)

    L_Selection = sorted(L_Selection)
    L_Clean=[]

    for fichier in L_Selection :
        Tempo = conversionInverse(fichier)
        L_Clean.append(Tempo)

    #L_Log = os.listdir("D://Clef64go/PJT/TEST2/")
    dfList=[]
    colnames=['No.','Date','Time','Temp1','Unit','Temp2','Unit','Lux2','Unit','BP1','Humidité Relat','Unit','CO2','Unit','Présence','Temp1_EnO','Unit','Temp2_EnO','Unit','Temp3_EnO','Unit','RH3_EnO','Unit','Chauffage','test']
    for filename in L_Clean:
        filename = pathIN + filename
        typefile=type(filename)
        df=pd.read_csv(filename, sep = ';', error_bad_lines=False, encoding="ANSI")
        dfList.append(df)

    concatDf=pd.concat(dfList,axis=0)
    concatDf.columns=colnames
    pathOUT = pathOUT + "/" + str(DateDebut) +" a "+ str(DateFin) + ".csv"
    concatDf.to_csv(pathOUT, sep = ';',index=False)


    df = pd.read_csv(pathOUT,decimal=",",sep = ';', error_bad_lines=False, encoding="ANSI",names=colnames)
    df['Temp1'] = [x.replace(',', '.') for x in df['Temp1']]
    df['Temp2'] = [x.replace(',', '.') for x in df['Temp2']]
    df['Temp1_EnO'] = [x.replace(',', '.') for x in df['Temp1_EnO']]
    df['Temp2_EnO'] = [x.replace(',', '.') for x in df['Temp2_EnO']]
    df['Temp3_EnO'] = [x.replace(',', '.') for x in df['Temp3_EnO']]


    date = df['Date']+df['Time']
    y1 = df['Temp1']
    y2 = df['Temp2']
    y3 = df['Temp3_EnO']
    y4 = df['Humidité Relat']
    y5 = df['CO2']
    y6 = df['Présence']



    #plotly.offline.plot({
    #    "data": [go.Scatter(x=x, y=y)],
    #        "layout": go.Layout(title="Température 1 en fonction du temps")
    #   }, auto_open=True)

    temp1 = go.Scatter(
        x=date,
        y=y1,
        name="Température 1 (°C)"
    )
    temp2 = go.Scatter(
        x=date,
        y=y2,
        name="Température 2 (°C)"
    )
    temp3 = go.Scatter(
        x=date,
        y=y3,
        name="Température 3 (°C)"
    )
    Humidite = go.Scatter(
        x=date,
        y=y4,
        name="Humidité relative (%)"
    )
    dioxyde_de_carbone = go.Scatter(
        x=date,
        y=y5,
        name="Taux C02 (ppm)"
    )
    presence = go.Scatter(
        x=date,
        y=y6,
        name="Présence"
    )


    fig = plotly.tools.make_subplots(rows=6, cols=1)

    fig.append_trace(temp1, 1, 1)
    fig.append_trace(temp2, 2, 1)
    fig.append_trace(temp3, 3, 1)
    fig.append_trace(Humidite, 4, 1)
    fig.append_trace(dioxyde_de_carbone, 5, 1)
    fig.append_trace(presence, 6, 1)



    fig['layout'].update(title='Représentation graphique des données')
    plotly.offline.plot(fig, filename=str(DateDebut) +" a "+ str(DateFin) + ".csv", auto_open=True)

这就是我得到的:enter image description here

3 个答案:

答案 0 :(得分:3)

您可以使用以下两行对其进行修复:

fig.update_xaxes(showticklabels=False) # hide all the xticks
fig.update_xaxes(showticklabels=True, row=6, col=1)

答案 1 :(得分:1)

我从安德鲁斯的评论开始。但是相关的来源是this one

EntityID

说明

您需要一次定义xaxis。为每个其他子图设置trace0 = go.Scatter( x = [0,1,1,0,0,1,1,2,2,3,3,2,2,3], y = [0,0,1,1,3,3,2,2,3,3,1,1,0,0] ) trace1 = go.Scatter( x = [0,1,2,3], y = [1,2,4,8], yaxis = "y2" ) layout = go.Layout( width = 800, height = 500, title = "fixed-ratio axes", xaxis = dict( nticks = 10, domain = [0, 0.45], title = "shared X axis" ), yaxis = dict( scaleanchor = "x", domain = [0, 0.45], title = "1:1" ), yaxis2 = dict( scaleanchor = "x", scaleratio = 0.2, domain = [0.55,1], title = "1:5" )) ,以便每个子图共享相同的scaleanchor = "x"。所有共享一个xasi的图都会自动绘制为子图,因此您可以摆脱当前的结构。

必需的编辑

xaxis

答案 2 :(得分:1)

这里的其他答案是解决方法。通过shared_xaxes method中的make_subplots参数,Plotly提供您正在寻找的功能:

fig = plotly.subplots.make_subplots(rows=6, cols=1, shared_xaxes=True)

您还可以选择与shared_yaxes=True共享y轴。但是,如果要共享两个坐标轴,则可能会发现只绘制一个图并向其添加多条迹线比较容易(与使用子图相比)。