1

result

   year  Month  Min_days    Avg_days    Median_days     Count
    2015   1          9        12.56666666          10         4
    2015   2          10       13.67678788          9          3    
   ........................................................
    2016   12       12       15.7889990           19          2
    and so on...

I wish to create a line plot plotting min_days, avg_days, median_days, count according to month and year say. how can I do that

Codes tried till now

axes = result.plot.line(subplots=True)
type(axes)

This works great but I m also getting subplots of year and month. I want year and month to be on x axis

Code2 tried:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
result.groupby('Year').plot(x='Year', y='Median_days', ax=ax, legend=False)

The issue with this code is it groupbys only year. I need month too (Jan 2015, Feb 2015 and so on). Also another issue is This gives me a single subplot for Median. How to add subplots for mean, min etc

Edit: P.S. Also if someone can answer in case month int is replaced with month names (Jan, Feb etc. it would be great)

1 Answer 1

1

One solution could be to first create a year-month column:

result["year-month"] = pd.to_datetime(
    result.year.astype(str) + "-" + result.Month.astype(str)
)

fig, ax = plt.subplots()
for col in ["Min_days", "Avg_days", "Median_days", "Count"]:
    ax.plot(result["year-month"], result[col], label=col)
ax.legend(loc="best")
ax.tick_params(axis="x", rotation=30)

With the limited data you've given us, you'll get: enter image description here

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

11 Comments

Hi sorry, the column is month name not month . Also the Year and Month name are indices not column names since result is a groupby output. So error I am getting is Keyword error: Year
@ShailajaGuptaKapoor then do .reset_index() first, and change .month to ['Month Name']
where should i do reset index ? Do you mean like this : result = result.reset_index()?
File "<ipython-input-94-9c6c8955ef21>", line 7 result.Year.astype(str) + "-" + result.['Month Name'].astype(str) ^ SyntaxError: invalid syntax
Edited the question now to reflect original'
|

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.