matplotlib 如何创建具有每年累计行驶距离的线图

bqjvbblv  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(186)

我想画出累计的旅行距离,每一年都有不同的颜色,而我纠结的是x轴,我想把它作为天数,这样你就可以看到每年的趋势以及它与前一年的偏差
我的数据集看起来像这样
| 日期|距离|
| - -----|- -----|
| 2010年1月1日|八|
| 2010年1月2日|5个|
| 2010年1月3日|六|
| 2010年1月4日|九个|
| 2010年1月5日|十二岁|
| 2010年1月6日|七个|
| 2010年1月7日|九个|
| 2010年1月8日|六|
我的代码看起来像这样

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('sample_data.csv')

df['date'] = pd.to_datetime(df['date'])

df_years = df[df['date'].dt.year.isin(range(2010, 2016))]
df_years = df_years.sort_values(by='date')
cumulative_distance = df_years.groupby(df_years['date'].dt.year)['distance'].cumsum()

colors= ['white', 'white', 'white', 'white', 'white', 'white']
for year, color in zip(range(2010,2016), colors):
    mask = (df_years['date'].dt.year == year)
    plt.plot(df_years.loc[mask, 'date'], cumulative_distance[mask], color=color, label=str(year))

plt.title('Cumulative Distance Traveled in 2010 - 2015')
plt.xlabel('Date')
plt.ylabel('Distance')
plt.legend()

plt.show()

我希望图表看起来像这样
真的很感激一些帮助,让它成为这种格式,因为我只是不能似乎解决它…我怀疑它的东西这么简单,我做错了。

我要找的图

正在生成的图

v2g6jxz6

v2g6jxz61#

你应该选择有意义的颜色,白色背景上的白色看起来是不可见的,并使用一个共同的信息作为X轴(如一年中的第几天):

import pandas as pd
import matplotlib.pyplot as plt

# sample dataframe
data = {'date': ['01/31/2009', '02/28/2009', '03/31/2009', '04/30/2009', '05/31/2009', '06/30/2009', '07/31/2009', '08/31/2009', '09/30/2009', '10/31/2009', '11/30/2009', '12/31/2009', '01/31/2010', '02/28/2010', '03/31/2010', '04/30/2010', '05/31/2010', '06/30/2010', '07/31/2010', '08/31/2010', '09/30/2010', '10/31/2010', '11/30/2010', '12/31/2010', '01/31/2011', '02/28/2011', '03/31/2011', '04/30/2011', '05/31/2011', '06/30/2011', '07/31/2011', '08/31/2011', '09/30/2011', '10/31/2011', '11/30/2011', '12/31/2011', '01/31/2012', '02/29/2012', '03/31/2012', '04/30/2012', '05/31/2012', '06/30/2012', '07/31/2012', '08/31/2012', '09/30/2012', '10/31/2012', '11/30/2012', '12/31/2012', '01/31/2013', '02/28/2013', '03/31/2013', '04/30/2013', '05/31/2013', '06/30/2013', '07/31/2013', '08/31/2013', '09/30/2013', '10/31/2013', '11/30/2013', '12/31/2013', '01/31/2014', '02/28/2014', '03/31/2014', '04/30/2014', '05/31/2014', '06/30/2014', '07/31/2014', '08/31/2014', '09/30/2014', '10/31/2014', '11/30/2014', '12/31/2014', '01/31/2015', '02/28/2015', '03/31/2015', '04/30/2015', '05/31/2015', '06/30/2015', '07/31/2015', '08/31/2015', '09/30/2015', '10/31/2015', '11/30/2015', '12/31/2015', '01/31/2016', '02/29/2016', '03/31/2016', '04/30/2016', '05/31/2016', '06/30/2016', '07/31/2016', '08/31/2016', '09/30/2016', '10/31/2016', '11/30/2016', '12/31/2016'],
        'distance': [83, 53, 34, 92, 45, 6, 99, 74, 27, 57, 37, 49, 29, 100, 96, 95, 4, 40, 40, 75, 44, 13, 78, 12, 28, 65, 89, 93, 63, 51, 64, 71, 19, 60, 78, 26, 55, 94, 47, 18, 64, 62, 23, 93, 22, 88, 39, 92, 13, 86, 90, 4, 16, 59, 90, 74, 17, 2, 85, 81, 64, 11, 71, 38, 55, 42, 6, 52, 35, 30, 46, 24, 74, 39, 84, 95, 40, 24, 64, 66, 8, 1, 32, 54, 3, 42, 31, 13, 62, 8, 15, 74, 40, 46, 11, 77]}

df = pd.DataFrame(data)

df['date'] = pd.to_datetime(df['date'])

df_years = (df[df['date'].dt.year.isin(range(2010, 2016))]
             .sort_values(by='date')
             .assign(cumulative_distance=lambda d: d.groupby(d['date'].dt.year)['distance'].cumsum())
            )

colors= ['black', 'blue', 'red', 'yellow', 'orange', 'green']
for year, color in zip(range(2010,2016), colors):
    mask = (df_years['date'].dt.year == year)
    plt.plot(df_years.loc[mask, 'date'].dt.day_of_year,
             df_years.loc[mask, 'cumulative_distance'],
             color=color, label=str(year))

plt.title('Cumulative Distance Traveled in 2010 - 2015')
plt.xlabel('Day of Year')
plt.ylabel('Distance')
plt.legend()

相关问题