简体   繁体   中英

3D Plotting in python, help for the z-axis

My objective is to make a chart like this (not with the same data but of the same type): 3D time series of opinion dynamics

Suppose I have a pandas DataFrame with 100 columns that contain values between [0,1], they are the same as 'Opinions' in the figure. The dataset df also has 500 rows, that represent the 'time' of the figure. For instance:

df:

Ind y0   y1  y2  y3  y4  y5  y6  ... 100 
0   0.7 0.9 0.2 0.0 0.0 0.9 0.9 0.9 0.9
1   0.8 0.9 0.2 0.0 0.0 0.9 0.9 1.0 0.9 
2   0.8 0.9 0.1 0.0 0.0 0.9 1.0 1.0 0.9 
... 0.9 0.9 0.1 0.0 0.0 0.9 1.0 1.0 0.9 
500 0.9 0.9 0.1 0 0 0.9 1.0 1.0 1.0 1.0     

If we consider the index as the x and the columns as y I have managed to plot them in 2D with the following code:


fig = plt.figure(figsize=(10,10))
ax = plt.axes(projection ='3d')  
for column in df:  
    ax.plot(df.index, df[column], marker='', color="dodgerblue", linewidth=1, alpha=0.9, label=column)

My result is the following: 2D plot

What I am struggling with is the z-axis. In my opinion, what I need is another dataset (let's call it df_1 ) where columns are filled with the count of df across the entire row. For instance, considering only 6 columns and 2 rows of the previous example, df_1 would be:

df_1:

Ind  z0  z1  z2  z3  z4  z5  z6 
0     1  3   1   2    2  3  3
1     1  3   1   2    2  3  3
2     1  2   1   2    2  2  1 

The first cell of the first row in df_1 is = 1 because in df[:,0] there was one-time 0.7. The second is 3 because df[:,0] has 3 times 0.9 and so on...

How could I build a dataset as df_1 in the example? or do you have any better ide on how to implement the z-axis in my chart?

Thanks in advance for the help

Solved with

def map_value_counts(row):
    vmap = row.value_counts().to_dict()
    return row.replace(vmap)

df_1 = df.apply(map_value_counts, axis=1)`

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