【问题标题】:Creating a Dashboard of Graphs in Python在 Python 中创建图形仪表板
【发布时间】:2022-01-20 11:32:22
【问题描述】:

在 Jupyter Notebook 中使用 matplotlib,我已经编写了代码来生成许多不同的图表(下面的示例代码用于创建我的一些图表)。我想将所有这些图表放入仪表板中。是否有适合我的实例的首选方法,其中我已经生成了图表并且只想将其插入仪表板模板?

似乎有许多不同的选项(散景和情节)。

# multiple line plots
plt.plot( 'Week', 'ESI_1_2_3', data=SZ_ED_TOT, marker='o', color='blue', linewidth=2)
plt.plot( 'Week', 'ESI_4_5', data=SZ_ED_TOT, marker='o', color='red', linewidth=2)
plt.ylabel('Patient Count')
plt.xlabel('Week')
plt.xticks(rotation = 90)
plt.title('SZ Acuity Counts')
plt.gcf().set_size_inches(20, 8)
plt.legend()
plt.show()

# multiple line plots
plt.plot( 'Week', '%_1_2_3', data=UMC_ED_TOT, marker='o', color='blue', linewidth=2)
plt.plot( 'Week', '%_4_5', data=UMC_ED_TOT, marker='o', color='red', linewidth=2)
plt.ylabel('% of Patients')
plt.xlabel('Week')
plt.xticks(rotation = 90)
plt.title('UMC Acuity % Of Total Patients')
plt.gcf().set_size_inches(20, 8)
plt.legend()
plt.show()

#dual Y axis graph
ax = UMC_Cobmined.plot.bar(x='Week', y='Total', rot=90,color=(0.2, 0.4, 0.6, 0.6))
ax.set_xlabel('Week')
ax.set_ylabel('Total Volume')

ax2 = ax.twinx()

ax2.plot(ax.get_xticks(),
   UMC_Cobmined[['%_ESI_3','%_ESI_4','%_ESI_5']].values,
   linestyle='-',
   marker='o', linewidth=2.0)
ax2.set_ylabel('% LWBS By Acuity')
ax2.legend(["%_ESI_3", "%_ESI_4","%_ESI_5"]);

plt.title('UMC LWBS Acuity % by Volume')
plt.gcf().set_size_inches(20, 8)
plt.show()

我希望仪表板的外观的理想示例:

【问题讨论】:

    标签: python pandas jupyter-notebook


    【解决方案1】:

    您可以通过在 matplotlib 中使用网格来实现:

    import matplotlib.pytplot as plt
    
    fig = plt.figure()
    grid = fig.add_gridspec(NUMBER_OF_GRID_COLUMNS, NUMBER_OF_GRID_ROWS)
    

    然后将每个图添加到一个轴上:

    ax1 = fig.add_subplot(grid[0, 0])
    # add plot to ax1 
    ax1.plot(x,y)
    ax1.title("Plot on ax1")
    
    ax2 = fig.add_subplot(grid[1,0])
    # add plot to ax2
    ax2.plot(x,y)
    ax2.title("Plot on ax2")
    
    plt.show() 
    

    等等

    【讨论】:

    • 抱歉,我是新手,因为我上面代码中的前两个图没有名称,我如何为它们分配名称以便将它们唯一地添加到轴?
    • 修改了答案——只需将绘图逻辑添加到轴上
    猜你喜欢
    • 2017-07-31
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2013-07-11
    • 2015-12-22
    • 2019-12-06
    • 1970-01-01
    相关资源
    最近更新 更多