第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何更正python matplotlib圖中x標簽的順序

如何更正python matplotlib圖中x標簽的順序

千萬里不及你 2022-11-29 15:33:32
我有以下要繪制的數(shù)據(jù)。month      year       total_salesMay         2020        7June        2020        2  July        2020        1August      2020        2September   2020        22 October     2020       11November    2020        6December    2020        3January     2019        3feburary    2019        11March       2019        65 April       2019        22May         2019        33June        2019        88July        2019        44August      2019        12September   2019        32October     2019        54November    2019        76December    2019        23January     2018        12feburary    2018        32March       2018        234April       2018        2432May         2018        432這是我用來執(zhí)行此操作的代碼:def plot_timeline_data(df):    fig, ax = plt.subplots()    ax.set_xticklabels(df['month'].unique(), rotation=90)    for name, group in df.groupby('year'):        ax.plot(group['month'], group['total_sales'], label=name,linestyle='--', marker='o')    ax.legend()    plt.tight_layout()    plt.show()我希望 x 標簽的順序從 1 月到 12 月開始,但我的圖表從 5 月到 12 月開始,然后從 1 月到 4 月恢復(fù),如圖所示(圖表的確切值因我更改值而不同)。我怎樣才能把它按正確的順序排列?
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經(jīng)驗 獲得超3個贊

您可以使用以下方法。這個想法是按照這個這篇文章中所示對月份列進行排序

# Capitalize the month names

df["month"] = df["month"].str.capitalize()


# Correct the spelling of February

df['month'] = df['month'].str.replace('Feburary','February')


# Convert to datetime object for sorting

df['month_in'] = pd.DatetimeIndex(pd.to_datetime(df['month'], format='%B')).month


# Sort using the index

df = df.set_index('month_in').sort_index()


plot_timeline_data(df)


查看完整回答
反對 回復(fù) 2022-11-29
?
牛魔王的故事

TA貢獻1830條經(jīng)驗 獲得超3個贊

Dataframe.plot讓你的工作更容易一些 - 它將每個系列繪制為不同的線,并保持你指定的順序:


import matplotlib.pyplot as plt


# Convert the dataframe to series of years

df = df.set_index(["month","year"])["total_sales"].unstack()

# Sort the index (which is month)

df = df.loc[[

    "January","feburary","March","April","May","June",

    "July", "August", "September","October", "November", "December"

]]

# Plot!

df.plot(marker="o", linestyle="--", rot=90)

# Show all ticks

plt.xticks(range(12), df.index)


查看完整回答
反對 回復(fù) 2022-11-29
  • 2 回答
  • 0 關(guān)注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號