如何为每个数据点分配独特的颜色

问题描述 投票:0回答:1
for index, row in df.iterrows():
        print(row['type'],row['type_id'])    
###########################################################
    color_index=df['type_id'].max()
    
    for index, row in df.iterrows():
        color_index = row['type_id'] % len(Category20)
        # Extract the time values for this event type as a Pandas Series object
        time_series = df['time']
        
        # Convert each Pandas Timestamp object to a Python datetime object, then to a Unix timestamp (seconds since 1970-01-01)
        timestamps = [t.to_pydatetime().timestamp() for t in time_series]

        # Compute the number of seconds since midnight for each timestamp by taking the modulo with 86400 (the number of seconds in a day)
        seconds_since_midnight = [ts % 86400 for ts in timestamps]

        # Create a ColumnDataSource object with the data for this event type
        source_data = dict(x=seconds_since_midnight,y=[0]*len(seconds_since_midnight),desc=time_series.dt.strftime('%Y-%m-%d %H:%M:%S'))

        for col_name in df.columns:

            if col_name.startswith('details'):

                source_data[col_name] = df[col_name].tolist()
        
        source = ColumnDataSource(data=source_data)       
        # Add a scatter plot glyph to the figure using the data from this event type's ColumnDataSource object and assign it a color from the Category10 palette and increase its size to 10 pixels.
        p.scatter('x', 'y', source=source, legend_label=row['type'],color=Category20[20][row['type_id']], size=10)

    
    #legend_labels = [item.label['value'] for item in p.legend.items]

    # Define tooltips dictionary containing label-value pairs for each column starting with 'details'
    tooltips_dict = {'Time': '@desc'}

    limited_df = df[['details.start_url','details.username']]
    for col_name in limited_df.columns:
       split_col = col_name.rsplit('.', 1)
       new_col = split_col[1] if len(split_col) > 1 else col_name
       tooltips_dict[new_col] = f': @{{{col_name}}}'
            
    # Add hover tool that displays all columns starting with 'details' when hovering over their dots
    hover_tool = HoverTool(tooltips=[(label, value) for label,value in tooltips_dict.items()])

    # Add hover tool and wheel zoom tool to our plot
    p.add_tools(hover_tool)

    # Remove tick lines on y-axis 
    p.yaxis.minor_tick_line_color = None 
    p.yaxis.major_tick_line_color = None 

    p.xaxis.minor_tick_line_color = None 
    p.xaxis.major_tick_line_color = None 

    # Show plot in web browser

    p.y_range = Range1d(y_min - 0.5, y_max + 0.5)
    p.yaxis.major_label_text_font_size = '0pt'
    show(p, width=1000, height=1000)
output of print(row['type'],row['type_id'])
session_created 1
leader_joined 2
control_gained 3
relocate_start 4
input_change 5
input_change 5
follower_joined 6
control_gained 3
control_gained 3
control_switch 7
host_change 8
control_gained 3
follower_joined 6
control_gained 3
click 9
input_change 5
input_change 5
session_end 10

但是所有的

datapoint
只有最后一个颜色

你能帮帮我吗

python pandas bokeh
1个回答
0
投票

您在示例代码中使用了

pandas
,因此我使用
bokeh
中DataFrames的集成支持创建了一个示例。

对于你的打印,我猜你的 DataFrame 看起来像这样

               type  type_id                   x  y
0   session_created        1 2023-05-17 00:00:00  0
1     leader_joined        2 2023-05-17 00:15:00  0
2    control_gained        3 2023-05-17 00:30:00  0
3    relocate_start        4 2023-05-17 00:45:00  0
4      input_change        5 2023-05-17 01:00:00  0
5      input_change        5 2023-05-17 01:15:00  0
6   follower_joined        6 2023-05-17 01:30:00  0
7    control_gained        3 2023-05-17 01:45:00  0
8    control_gained        3 2023-05-17 02:00:00  0
9    control_switch        7 2023-05-17 02:15:00  0
10      host_change        8 2023-05-17 02:30:00  0
11   control_gained        3 2023-05-17 02:45:00  0
12  follower_joined        6 2023-05-17 03:00:00  0
13   control_gained        3 2023-05-17 03:15:00  0
14            click        9 2023-05-17 03:30:00  0
15     input_change        5 2023-05-17 03:45:00  0
16     input_change        5 2023-05-17 04:00:00  0
17      session_end       10 2023-05-17 04:15:00  0

在下面的代码中,我使用

map()
将颜色映射到每个 type_id。

然后我将 DataFrame 转换为 ColumnDataSource。在最后一步中,我仅使用源信息绘制散点图。

import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.palettes import Category20
from bokeh.plotting import figure, show, output_notebook
output_notebook()

df = pd.DataFrame({
    'type':["session_created",
            "leader_joined",
            "control_gained",
            "relocate_start",
            "input_change",
            "input_change",
            "follower_joined",
            "control_gained",
            "control_gained",
            "control_switch",
            "host_change",
            "control_gained",
            "follower_joined",
            "control_gained",
            "click",
            "input_change",
            "input_change",
            "session_end"
           ],
    "type_id": [1,2,3,4,5,5,6,3,3,7,8,3,6,3,9,5,5,10],
    'x':pd.date_range('2023-05-17', freq='15T', periods=18),
    'y':0
})
# map each unique id to a color
df['color'] = df["type_id"].map({i:v for i, v in enumerate(Category20[20])})


p = figure(x_axis_type="datetime")
p.scatter(x='x', y='y', color='color', legend_group='type', source=ColumnDataSource(df))
show(p)

© www.soinside.com 2019 - 2024. All rights reserved.