0

I have number of dates and i want to show only current five dates plots on bar. I required slider for looking the dates other than current five dates.

My data is huge and when plotted, it is so messy to see. So I am trying to use X axis slider, so that, an user can slide over the x axis and visualize it easily .

import matplotlib.pyplot as plt import numpy as np import ipywidgets as wg

conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};" "Server=RUSHABH\RUSHABHPC;" "Database=SCADA;" "Trusted_Connection=yes;") daily_data = pd.read_sql_query('SELECT * FROM [SCADA].[dbo].[TOTALIZER] order by SDATETIME ASC', conn)

class Dashboard: def init(self, window): self.window = window self.window.title('Dashboard') self.window.geometry('1366x768') self.window.state('zoomed') self.window.config(background='#eff5f6')

     datadaily = daily_data.groupby([pd.Grouper(key="SDATETIME", freq="D")]).sum()#daily_data.groupby(pd.to_datetime(daily_data['SDATETIME']).dt.strftime('%y-%m-%d'))['FE014CPV', 'CCC'].last().reset_index()
    datadaily.index = datadaily.index.strftime("%d-%m")#lastdayfrom = daily_data['SDATETIME'].max()

sliders do not support datetime display, but it is possible to display timestamps. Here is a full example (I didn't base it off of your code, because you did not provide any data M = max(datadaily['FE014CPV'])

    N = 5


    figure3 = Figure(dpi=100)
    ax3 = figure3.add_subplot(111)
    ax3.set_title('Daily Steam Generation (Ton) Vs Coal Consumption (Ton)')
    z = np.arange(len(datadaily.index))#date2num(datadaily.index)
    ax3.bar(z - BarWidth/4, datadaily['FE014CPV'], width=BarWidth/2, facecolor='indianred', align='center')
    ax3.bar(z + BarWidth/4, datadaily['CCC'], width=BarWidth/2, facecolor='#7eb54e', align='center')
    ax3.set_ylabel('Steam Generation(Ton)')
    ax3.set_xticks(z)
    ax6 = ax3.twinx()  # Create another axes that shares the same x-axis as ax.
    ax6.set_ylim(*ax3.get_ylim())
    ax6.set_ylabel('Coal Consumption(Ton)')
    ax3.set_xticklabels(datadaily.index, rotation=90, fontsize=6)
    ax3.legend(['Steam Generation', 'Coal Consumption'])
    #ax3.xaxis_date()
    #ax3.autoscale(tight=True)
    bar3 = FigureCanvasTkAgg(figure3, self.window)
    bar3.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    bar3.get_tk_widget().place(x=760, y=400, width=600, height=300)

    ax_time = bar3.add_axes([0.12, 0.1, 0.78, 0.03])
    s_time = Slider(ax_time, 'Channels', 0, len(z) - N, valinit=0, valstep=1)

    def update(val):
        N = 5
        pos = s_time.val
        ax3.axis([pos - 1 / 2, pos + N - 1 / 2, 0, M + 1])
        bar3.canvas.draw_idle()

    s_time.on_changed(update)

I'm trying to put a slider right under the x-axis of a subplot in matplotlib

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 05 '23 at 17:00

0 Answers0