0

I am trying to plot the number of songs listened to by day, I have a loop that deletes entries where the value is zero, but I still see zeroes on the plot.

Here is my loop that deletes entries with values of zero:

real_dr = set(tbday.keys())
for day in real_dr:
    if tbday[day] == 0:
        del tbday[day]

Here is a printout of the first entries of the dict: printout of dict keys

Here is the calplot created: partial calplot screenshot

As you can see, it's hard to distinguish between the low numbers such as 1 and the zeroes in the middle.

How do I plot this with gray spots (like the beginning of the top year) instead of the zeroes?

I have tried setting the zero values to None and removing them altogether with no change in the plot

Update:

After some investigation, I see that it renders fine, like in the screenshot below if and only if there are less than or equal to 516 items in the dictionary the DataFrame pulls from screenshot of what it should look like

If there are more than 516 entries in that dict, the graph looks like the first one (not the desired look). I'm not sure if this is relevant information, but it struck me as odd

Brett
  • 1
  • 1

2 Answers2

0

You can use a dictionary comprehension to filter out any zero objects from a dictionary

It looks like you're using data of the form:

import datetime
from pprint import pprint

data = {
    datetime.date(2016, 5, 12): 11,
    datetime.date(2016, 5, 13): 20,
    datetime.date(2016, 5, 14): 0,
    datetime.date(2016, 5, 16): 1,
    datetime.date(2016, 5, 17): 5,
}
Filtering a dictionary to make sure the values are > 0
filtered = {date_: data[date_] for date_ in data if data[date_] > 0}
pprint(filtered)

# {datetime.date(2016, 5, 12): 11,
#  datetime.date(2016, 5, 13): 20,
#  datetime.date(2016, 5, 16): 1,
#  datetime.date(2016, 5, 17): 5}

It works

  • This did not work for me. I have already filtered out the data and have seen the dictionary does not have any zero values in it, but the calplot still displays with those values if the dataframe passed in was large enough (that is what I'm observing, but I'm unsure of the underlying reason) – Brett Mar 01 '23 at 22:11
0

There is a "dropzeros" parameter to this function that must be passed in True for this to happen, per the doc: https://calplot.readthedocs.io/en/latest/

Brett
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xlmaster Mar 17 '23 at 18:31