1

I'm working in a Jupyter notebook and I'm trying to plot this Pickled dataframe

index       value
------------------
2020-10-19  3
2020-10-26  4
2020-11-02  3
2020-11-09  2
2020-11-16  1
2020-11-23  3
2020-11-30  1
2020-12-07  4
2020-12-14  6
2020-12-21  3
2020-12-28  1
2021-01-04  1
2021-01-11  5
2021-01-18  10
2021-01-25  9
2021-02-01  8
2021-02-08  12
2021-02-15  6

I am using this code (divided in different cells, but irrelevant to say):

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

plt.style.use('ggplot')

data = pd.read_pickle('data.pkl')

fig, ax = plt.subplots(figsize=(10,10))

weekly = data.resample('W-MON').sum()
ax.plot(weekly)

labels = [f"{d.day}-{d.month}-{d.year}" for d in weekly.index]
ax.set_xticklabels(labels, rotation=45)

It produces this plot, but it's missing the latter half of the data:

enter image description here

5
  • I don't think the values are missing. It seems like your graph doesn't match the data provided. The problem might come from your labels. Commented Feb 17, 2021 at 9:06
  • @Jao even if I comment that line, the labels look ugly but they are still the same and same amount Commented Feb 17, 2021 at 9:07
  • 2
    What I mean is that the data seems to be plotted correctly. The problem is not the data it's the labels that don't match them. Commented Feb 17, 2021 at 9:08
  • @Jao I have checked, in fact they don't match. that's because the ticks are half the length of the data. I need all the ticks to match the index of my data. I can't figure out how Commented Feb 17, 2021 at 9:15
  • It is almost certainly because of weeklydoesn't match your data. You used resamplingand from what I have read it will here merge data by week. So you have a maximum of one label per week. Because some of your labels were merged togethers some are missing. You should either not resample your labels or check what is plotted by `ax.plot(weekly) it is not the aggregated data. Commented Feb 17, 2021 at 9:20

1 Answer 1

2

Problem was that the ticks amount were automatically generated by the plot, thus being half the amount I needed. It was sufficient to specify how many via ax.set_xticks()

ax.set_xticks(weekly.index)  # <--
labels = [f"{d.day}-{d.month}-{d.year}" for d in weekly.index]
ax.set_xticklabels(labels, rotation=45)

Sometimes the smaller problems are those that cause more confusion, but oh well...

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.