Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the x-axis values while subplotting with plotly

I'm using plotly to plot a large csv file (with a lot of data) and to subplot in line (which works actually) some charts. The problem is that for each chart I get all of the date+time for each point on the x-axis resulting in a bad quality chart. How can I hide for the 5 first chart the values of the x-axis while keeping it for the last one to have a better vieaw of the charts ?

Here is my code : the problematic part start at line 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)

Here is what I get :

enter image description here

like image 204
Benoit Avatar asked Dec 06 '22 09:12

Benoit


2 Answers

You can fix it with these two lines:

fig.update_xaxes(showticklabels=False) # hide all the xticks
fig.update_xaxes(showticklabels=True, row=6, col=1)
like image 110
Piotro Avatar answered Jan 05 '23 00:01

Piotro


The other answers here are workarounds. Plotly offers the functionality you're looking for with the shared_xaxes parameter in the make_subplots method:

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

You can also choose to share the y axes as well with shared_yaxes=True. However if sharing both axes is your intent, you may find it easier to have a single plot and just add multiple traces to it (vs using subplots).

like image 44
Brendan Avatar answered Jan 04 '23 23:01

Brendan