0

I have made a chart using matplotlib in Python. I am trying to figure out how to make my Y Axis label more vague as you are zoomed out and more detailed when it is zoomed in. For example, if I am zoomed out all the way, I would like the label to show year and month as in 07/20 and 08/20. When you zoom in more it would show the days as well. 07/01/2020. When you get really zoomed in, you would be able to see date and time. Here is what my data looks like

i,datetime,count
0,2020-07-04 17:15:00,1
1,2020-07-04 17:30:00,1
2,2020-07-04 17:35:00,2
3,2020-07-04 17:45:00,2
4,2020-07-04 18:10:00,1

Here is what my chart looks like. What the chart looks like

Here is my code that is generating the chart.

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y %H:%M'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.grid(True)
plt.plot(df['date_end'], df['count'], color='black');
plt.show()

Can someone point me toward the right direction?

pcct2020
  • 61
  • 8

1 Answers1

0

To dynamically change the x-axis, the 'plotly' graph is the easiest way to achieve the objective. I have created sample data and suggested an example where you can move the x-axis of a time series with a slider. The reference is the official reference. The introduction to 'plotly' can be found here

import pandas as pd
import numpy as np
np.random.seed(20200905)

value = np.random.randint(0,3000, 300)
date_rng = pd.date_range('2020-7-04 09:00:00', freq='5min', periods=300)
df = pd.DataFrame({'datetime':date_rng,'value':value})

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(x=df['datetime'], y=df['value'], mode='lines', name='lines'))

# Add range slider
fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=12,
                     label="12hour",
                     step="hour",
                     stepmode="backward"),
                dict(count=1,
                     label="1day",
                     step="day",
                     stepmode="backward")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)

fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32