数据框的箱线图绘制不正确

2024-09-27 00:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着根据不同的数据帧绘制许多箱线图,结果发现尽管我为每个箱线图定义了一个新的图形,但每个图都是在以前的箱线图上绘制的(所以只有第一个图是正确绘制的)。你知道吗

我创建的数据帧:

cpu_util=pd.DataFrame.from_dict(cpu_util)
gpu_util=pd.DataFrame.from_dict(gpu_util)
cpu_mem=pd.DataFrame.from_dict(cpu_mem)
gpu_mem=pd.DataFrame.from_dict(gpu_mem)
disk_c_usage=pd.DataFrame.from_dict(disk_c_usage)
disk_c_fs=pd.DataFrame.from_dict(disk_c_fs)
disk_d_usage=pd.DataFrame.from_dict(disk_d_usage)
disk_d_fs=pd.DataFrame.from_dict(disk_d_fs)

以及基于上述数据帧的箱线图创建:

    boxplot =cpu_util.boxplot(rot=90,fontsize=4,grid=True,showfliers=False,whis=[0,100])
    fig1 = boxplot.get_figure()
    fig1.suptitle('CPU utilization [%]', fontsize=10, fontweight='bold')
    boxplot.set_ylabel('Utilization [%]')
    boxplot.set_xlabel('Computer name')
    boxplot.set_ylim(0,100)
    fig1.savefig('cpu_util.pdf')


        #### gpu util ####
    boxplot2 = gpu_util.boxplot(rot=90,fontsize=4,grid=True,showfliers=False,whis=[0,100])
    fig2 = boxplot2.get_figure()
    fig2.suptitle('GPU utilization [%]', fontsize=10, fontweight='bold')
    boxplot2.set_ylabel('Utilization [%]')
    boxplot2.set_xlabel('Computer name')
    boxplot2.set_ylim(0,100)
    fig2.savefig('gpu_util.pdf')


        #### cpu mem ####
    boxplot3 = cpu_mem.boxplot(rot=90,fontsize=4,grid=True,showfliers=False,whis=[0,100])
    fig3 = boxplot3.get_figure()
    fig3.suptitle('CPU memory [%]', fontsize=10, fontweight='bold')
    boxplot3.set_ylabel('Memory [%]')
    boxplot3.set_xlabel('Computer name')
    boxplot3.set_ylim(0,100)
    fig3.savefig('cpu_memory.pdf')

我总共创建了6个箱线图(复制到这里只是其中的一部分,但是想法很清楚……)

第一个箱线图的结果(正确绘制):

first boxplot

第二个(如前所述,绘制在第一个框上,每个框可以看到两条绿线,而不是一条线):

second boxplot

数据帧构建不是问题所在。我检查了一下,它们的值是正确的。你知道吗

经过大量的检查,我想问题出在图形定义上,但没有指出问题所在。你知道吗


Tags: fromdataframegpuutil绘制cpumemdict
2条回答

如果要创建6个图形,可以使用for循环并在循环中创建每个图形。你知道吗

  for  i in [cpu_util, gpu_util, cpu_mem, gpu_mem]:
      fig, ax= plt.subplots()
      ax.boxplot(i)
      ax.set_title('CPU utilization [%]', fontsize=10, fontweight='bold')
      ax.set_ylabel('Utilization [%]')
      ax.set_xlabel('Computer name')
      ax.set_ylim(0,100)
      fig.savefig('cpu_util.pdf',dpi=300)

上面的代码为每个数据帧创建一个图形。如果你想把它们放到子图中,你可以使用下面的代码。你知道吗

  c=0 # simple counter
  for  i in [cpu_util, gpu_util, cpu_mem, gpu_mem]:
      row= c//2 # since we have 2 columns row will be either 0 or 1.
      col= c%2 # since we have 2 columns columns also will be either 0 or 1.
      fig, ax= plt.subplots(2,2) # 2 rows and 2 columns.
      ax[row,col].boxplot(i)
      ax[row,col].set_title('CPU utilization [%]', fontsize=10, fontweight='bold')
      ax[row,col].set_ylabel('Utilization [%]')
      ax[row,col].set_xlabel('Computer name')
      ax[row,col].set_ylim(0,100)
      fig.savefig('cpu_util.pdf',dpi=300)
      c+=1

如果有什么不清楚,请告诉我。你知道吗

有几种方法:

  1. 在每个fig.savefig()行之后添加plt.clf()。除非清除绘图,否则程序将覆盖已经存在的任何绘图。plt.clf()清除绘图并允许您重新开始下一个图像。你知道吗

    即:

    boxplot =cpu_util.boxplot(rot=90,fontsize=4,grid=True,showfliers=False,whis=[0,100])
    fig1 = boxplot.get_figure()
    fig1.suptitle('CPU utilization [%]', fontsize=10, fontweight='bold')
    boxplot.set_ylabel('Utilization [%]')
    boxplot.set_xlabel('Computer name')
    boxplot.set_ylim(0,100)
    fig1.savefig('cpu_util.pdf')
    plt.clf()
    
  2. 看看这个例子:matplotlib examples

    通过使用plt.subplots(),您可以创建3个不同的绘图子绘图,然后将它们保存为一个图像。当然,你可以为6个情节做类似的事情。

  3. 你可以把plt.figure()放在每个新绘图的开头,这将从头开始创建一个图形。

相关问题 更多 >

    热门问题