数据常用code - 画图

# plotly
mode:该参数用于指定绘图模式,比如折线图('lines')、散点图('markers')等。
name:设置图例中的名称。
marker:用于设置数据点的样式,比如颜色、大小和符号。
line:用于设置线条的样式,比如颜色、宽度和样式。
text:用于指定数据点的文本标签。
hoverinfo:设置鼠标悬停时显示的信息,可以包括数据值、标签等。
layout:用于设置整个图表的布局信息,包括标题、轴标签、背景颜色等。
title:用于设置图表的标题。
barmode:该参数用于指定并列柱状图的显示方式,比如堆叠('stack')或平铺('group')。
color:用于设置图表的整体颜色主题。
template:设置图表的整体模板,包括背景样式、字体等。

折线

单条

import plotly.graph_objects as go

# 创建一个空的图像对象
fig = go.Figure()

# 添加折线图
fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 3, 2, 3, 1],
    mode='lines+markers',
    name='折线图示例',
    marker=dict(color='red', size=10),
    line=dict(color='blue', width=2),
    text=['A', 'B', 'C', 'D', 'E'],
    hoverinfo='text'
))

# 设置图表布局和样式
fig.update_layout(
    title='示例折线图',
    xaxis_title='X 轴',
    yaxis_title='Y 轴',
    width=800,
    height=400,
    #plot_bgcolor='lightgrey',
    #template='plotly_dark'
)

# 显示图表
fig.show()

 多条

# 两条折线对比
plt.figure(figsize=(10, 4))
sns.lineplot(x='updatedat', y='from_cnt', data=result)
line_avg_loc = plt.plot(result['updatedat'], result['from_cnt'], color='steelblue',label='Forward Task')
line_loc_before_open = plt.plot(result['updatedat'], result['return_cnt'], color='indianred',linestyle='dashed',label='Return Task')
lines = line_avg_loc + line_loc_before_open
labels = [line.get_label() for line in lines]

# 计算并绘制均值线
mean_value = result['pickedbyusername'].mean()
plt.axhline(mean_value, color='red', linestyle='--', label='Mean')

plt.legend(lines, labels)
plt.xlabel('Hour')
plt.ylabel('Task Count')
plt.title(f'Forward and Return Task Distribution by hour')
plt.show()

 带水平线

 

 条形图

# 循环画图,同时显示折线和柱状图,高亮标注open_day
# 示例:transport_analysis
for part in part_list_incre:
    fig = make_subplots(specs=[[{"secondary_y": True}]])
    list_data = raw_data[raw_data['part_number']==part]
    plt_data = process_data(list_data)
    plt_data = plt_data[['shipmentnumber', 'ship_date', 'delivery_date', 'part_number','is_pickup_delay', 'is_delivery_delay']].drop_duplicates()
    
    plt_data = plt_data.groupby(['ship_date', 'part_number']).agg(
        shp_cnt=('shipmentnumber', 'count'),
        pickup_delay_cnt=('is_pickup_delay', 'sum'),
        delivery_delay_cnt=('is_delivery_delay', 'sum')
    ).reset_index()

    plt_data = plt_data.groupby('ship_date').agg(
        shp_cnt = ('shp_cnt','sum'),
        pickup_delay_cnt = ('pickup_delay_cnt','sum'),
        delivery_delay_cnt = ('delivery_delay_cnt','sum'),
    ).reset_index()
    plt_data['delivery_rate'] = plt_data['delivery_delay_cnt']/plt_data['shp_cnt']
    plt_data['pickup_rate'] = plt_data['pickup_delay_cnt']/plt_data['shp_cnt']
    fig.add_trace(go.Bar(x=plt_data['ship_date'], y=plt_data['shp_cnt'], name='inventory', marker_color="skyblue"), secondary_y=False)
    fig.add_trace(go.Line(x=plt_data['ship_date'], y=1-plt_data['pickup_rate'], mode='lines',  marker_color="orange", name='Pickup Ontime'), secondary_y=True)
    fig.add_trace(go.Line(x=plt_data['ship_date'], y=1-plt_data['delivery_rate'], mode='lines',  marker_color="red", name='Delivery Ontime'), secondary_y=True)
    
    for day in openday:
        fig.add_shape(dict(type="line", x0=day, y0=0, x1=day, y1=10, line=dict(color="Black", width=2, dash="dash")))
    fig.update_layout(xaxis_title='ship_date', yaxis_title='shp_cnt', legend=dict(x=0, y=1.2))
    fig.update_layout(title_text=f"Delivery Count {part}", title_x=0.5)
    fig.update_yaxes(title_text="Ontime Rate", secondary_y=True)
    fig.show()
# 条形图对比
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# 创建数据
labels = ['>1day', '0~3h', '3h~1day']
total_counts = [224, 657, 1143]
delay_ratio = [2.54, 42.79, 55.11]

# 创建子图
fig = make_subplots(rows=1, cols=2)

fig.add_trace(go.Bar(x=labels, y=total_counts, name='Total Count'), row=1, col=1)
# 添加第二个条形图
fig.add_trace(go.Bar(x=labels, y=delay_ratio, name='Delay Count'), row=1, col=2)

# 更新布局
fig.update_layout(title_text='Total Count and Delay Count', xaxis_title='Type', showlegend=True)

# 显示图表
fig.show()

箱线图

# 箱线图对比
import plotly.graph_objects as go
# 假设 df_plt 是包含了 shipment_ship_date 和 return_diff_mean 的 DataFrame
start_date = '2024-01-06'
end_date = '2024-01-15'
df_subset = df_plt[(df_plt['shipment_ship_date'] >= start_date) & (df_plt['shipment_ship_date'] <= end_date)]

# 创建箱线图
fig = go.Figure()
plt  ='acquiretime_diff_max'
# 使用 groupby 和 aggregate 函数计算每个日期上的 return_diff_mean 的统计数据
grouped_data = df_subset.groupby('shipment_ship_date')[plt].agg(list)

# 将统计数据绘制成箱线图
for date, values in grouped_data.items():
    fig.add_trace(go.Box(y=values, name=str(date)))

# 更新图表布局
fig.update_layout(
    title = f'{plt} by shipment_ship_date',
    xaxis_title='Shipment Ship Date',
    yaxis_title='Return Difference Mean',
    width=800,
    height=600
)

# 显示图表
fig.show()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值