1

I have a timeseries of data I would like to plot. In the night, when i do not collect data, I have a gap between 9 pm and 7 am which looks a bit ugly on the chart and makes it hard to read.

here is a little example to understand the issue:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


df2 = pd.DataFrame({ 'A' : pd.Series(np.random.randn(4),index=list(range(4)),dtype='float32'),
                    'B' : pd.date_range('1/1/2000', periods=4)})




print(df2.to_string())
df2.ix[3,'B'] = pd.to_datetime('2005-01-02')

print(df2.to_string())

df2.index = df2.B
fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(df2.index, df2["A"])
plt.show()

the graph from 1/1/2000 to 1/3/2000 is almost unreadable, because the plot is scaled to show also the data from 2005. is there a way to eliminate that the indices (?) from 1/3/2000 to 1/3/2005?

Thanks and cheers, E.

7
  • you can eliminate it if the x-axis is a string type.. so your example above you'll do df2.index = df2.index.astype(str) Commented Nov 1, 2018 at 13:14
  • Hi and thanks. This throws me an error ValueError: could not convert string to float: '2005-01-02' .. cheers, E. Commented Nov 1, 2018 at 13:16
  • your index is already a string? can we see a sample of your actual data ? The above works for the example you provided where the index B is a datetime Commented Nov 1, 2018 at 13:19
  • oh I mean I get an error with the above example ... I'll check again Commented Nov 1, 2018 at 13:20
  • @gyx-hh where would i put that line? I guess after assignen B to the index, but then ax.plot complains Commented Nov 1, 2018 at 13:23

1 Answer 1

4

IIUC, let me create a sample set and bad outcome.

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))
df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]
df2.plot()

Output:

enter image description here

However, we can eliminate those flatline sections like this:

np.random.seed(0)
df = pd.DataFrame(np.random.random(500), index=pd.date_range('2018-11-25 07:00:00', periods=500, freq='10T'))

df2 = df[(df.index.hour >= 7) & (df.index.hour < 21)]

df2.index = df2.index.strftime('%Y-%m-%d')

fig, ax = plt.subplots()
_ = df2.plot(ax=ax)
skip = df2.shape[0]//7 + 1
label = [i for i in df2.index[::skip]]
_ = plt.xticks(np.arange(0,df2.shape[0],skip),label,rotation=45)

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.