亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

以列中繪制 x 軸的熊貓分組

以列中繪制 x 軸的熊貓分組

泛舟湖上清波郎朗 2022-09-20 17:20:56
我有一個時間序列的數據幀,其中列是時間值(按順序),每行都是一個單獨的序列。我還有額外的列,提供每行的類別,這反過來又決定了線型和顏色。下面是數據幀:>>> df                cat (frac_norm, 2, 1)                                                                                                       clustermonth_rel                           -5        -4        -3        -2        -1         0         1          2         3         4          5        user1   user2                                                                                                                                       3414845 4232621  -1b          0.760675  0.789854   0.95941  0.867755  0.790102         1  0.588729   0.719073  0.695572  0.647696   0.656323       44369232 3370279  -1b          0.580436  0.546761   0.71343  0.742033  0.802198  0.389957  0.861451   0.651786  0.798265  0.476305   0.896072       022771   3795428  -1b          0.946188  0.499531  0.834885  0.825772  0.754018   0.67823  0.430692   0.353989  0.333761  0.284759   0.260501       22660226 3126314  -1b          0.826701   0.81203  0.765182  0.680162  0.763475  0.802632         1   0.780186  0.844019  0.868698   0.722672       44154510 4348009  -1b                 1  0.955656  0.677647  0.911556   0.76613  0.743759   0.61798   0.606536  0.715528  0.614902   0.482267       32860801 164553   -1b          0.870056  0.371981  0.640212  0.835185  0.673108  0.536585         1   0.850242  0.551198  0.873016   0.635556       4120577  3480468  -1b            0.8197  0.879873  0.961178         1  0.855465  0.827824  0.827139   0.304011  0.574978  0.473996   0.358934       3我可以制作下面的圖,其中x軸是 的有序值,顏色取決于 的值,線型取決于 的值。但是,它是逐行的。有沒有辦法對此進行矢量化,例如,通過使用groupby?('frac_norm',2,1)clustercat用于生成圖像的代碼import pandas as pdimport numpy as npcolors = ['r','g','b','c','y','k']lnst = ['-','--']cats = np.sort(df['cat'].unique())clusters = np.sort(df['cluster'].unique())colordict = dict(zip(clusters, colors))lnstdict = dict(zip(cats,lnst))
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

你應該能夠使用熊貓來繪制并避免循環:


from matplotlib.colors import LinearSegmentedColormap


colors = ['r','g','b','c','y','k']

lnst = ['-','--']

cats = np.sort(df['cat'].unique())

clusters = np.sort(df['cluster'].unique())

colordict = dict(zip(clusters, colors))

lnstdict = dict(zip(cats,lnst))


# transpose data frame

df1 = df.T


# map colors from colordict to cluster

cmap = df['cluster'].map(colordict).values.tolist()

# create a custom color map and line style

lscm = LinearSegmentedColormap.from_list('color', cmap)

lstyle = df['cat'].map(lnstdict).values.tolist()


# plot with pandas 

df1.iloc[1:12].reset_index(level=0, drop=True).plot(figsize=(20,10),

                                                    colormap=lscm,

                                                    style=lstyle)

http://img1.sycdn.imooc.com//632986330001310b16590841.jpg

更新(假設您希望兩者都在同一圖表上)

from matplotlib.colors import LinearSegmentedColormap

import matplotlib.pyplot as plt

%matplotlib inline


colors = ['r','g','b','c','y','k']

lnst = ['-','--']

cats = np.sort(df['cat'].unique())

clusters = np.sort(df['cluster'].unique())

colordict = dict(zip(clusters, colors))

lnstdict = dict(zip(cats,lnst))


# transpose data frame

df1 = df.T


# map colors from colordict to cluster

cmap = df['cluster'].map(colordict).values.tolist()

# create a custom color map and line style

lscm = LinearSegmentedColormap.from_list('color', cmap)

lstyle = df['cat'].map(lnstdict).values.tolist()


c = df.columns


# not needed for your actually dataframe

# i am just converting your sample data to numeric

for i in range(len(df.columns[1:])-1):

    df[c[i+1]] = pd.to_numeric(df[c[i+1]])


# groupby and get mean of cluster

df2 = df[c[1:]].groupby('cluster').mean()



# create sublots object from matplotlib

fig, ax = plt.subplots()

# add a twin y-axis

ax2 = ax.twiny()


# plot dataframe 1

df1.iloc[1:12].reset_index(level=0, drop=True).plot(ax=ax, figsize=(20,10),

                                                    colormap=lscm,

                                                    style=lstyle)


# create legend for ax

handles, labels = ax.get_legend_handles_labels()

ax.legend(handles, labels, loc='center left', borderaxespad=-20)


# subplot df2

df2.plot(ax=ax2, colormap='copper')


# create legend for ax2

handles, labels = ax2.get_legend_handles_labels()

ax2.legend(handles, labels, loc='center right', borderaxespad=-20)

http://img1.sycdn.imooc.com//632986460001aeb516600668.jpg

查看完整回答
反對 回復 2022-09-20
  • 1 回答
  • 0 關注
  • 76 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號