0

I have a following problem. I have these data:

import pandas as pd, numpy as np

data = pd.DataFrame(
    {
        "year": [2000, 2001, 2001, 2001, 2002, 2003, 2004, 2005, 2006, 2007],
        "week": [0, 1, 2, 2, 3, 3, 4, 5, 6, 7],
        "value": [8, 1, 53, 2, 55, 3, 4, 55, 60, 76],
    }
)

Then I plot the data week_groups = data.groupby([data["year"], data["week"]])["value"].count()

Now I would like to add a vertical line in (2004, 4). I try plt.axvline(x=(2004, 4)) but I got an Error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

This does not help either:

plt.axvline(data["year"][np.where(np.array(data["year"]) == 2004)[0][0]])
week_groups.plot(kind="bar", figsize=(10, 5))
plt.show()

How can I fix it please?

4
  • 1
    You must specify the index of (2020,20) for x, since (2020,20) is only the tick label Commented Jun 27, 2021 at 10:34
  • Try plt.axvline(x=str((2020, 20))) Commented Jun 27, 2021 at 10:35
  • thanks for answer. @CrynetOmega how can I do it please? Commented Jun 27, 2021 at 10:37
  • @ShaunHan does not help. It just put the line in the first index that is (2012, 13) Commented Jun 27, 2021 at 10:38

2 Answers 2

1

This might help.

# coding: utf-8
import pandas as pd
from matplotlib import pyplot as plt

data = pd.DataFrame(
    {
        "year": [2000, 2001, 2001, 2001, 2002, 2003, 2004, 2005, 2006, 2007],
        "week": [0, 1, 2, 2, 3, 3, 4, 5, 6, 7],
        "value": [8, 1, 53, 2, 55, 3, 4, 55, 60, 76],
    }
)

week_groups = data.groupby([data["year"], data["week"]])["value"].count()

for i in range(len(week_groups.index)):
    if week_groups.index[i] == (2004,4):
        plt.axvline(i, color='skyblue')
# Apparently, i can only solve this by a for loop. 
# Tried every other indexing method. But, no avail. 
# If any one can come up with a shorter / better code, you are welcome.
week_groups.plot(kind="bar", figsize=(10, 5))
plt.show()

Output below:

Output

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

1 Comment

Nice! here is the one liner: plt.axvline([i for i in range(len(week_groups)) if week_groups.index[i]==(2004,4)][0], color='skyblue')
0

I don't know exactly how your groupby object is structured, so I've written a little example that shows how to get a specific index from a dataset and plot a vertical line based on it.

import pandas as pd, numpy as np

data = {
  "year": [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007],
  "week": [0, 1, 2, 3, 4, 5, 6, 7]
}
plt.axvline(data['year'][np.where(np.array(data['year']) == 2004)[0][0]])
plt.plot(data['year'], data['week'])

1 Comment

I improved my question, please look on reproducible example. Thanks a lot

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.