简体   繁体   中英

Line plot x axis using pandas python

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: PS Also if someone can answer in case month int is replaced with month names (Jan, Feb etc. it would be great)

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM