我试着根据不同的数据帧绘制许多箱线图,结果发现尽管我为每个箱线图定义了一个新的图形,但每个图都是在以前的箱线图上绘制的(所以只有第一个图是正确绘制的)。你知道吗
我创建的数据帧:
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个箱线图(复制到这里只是其中的一部分,但是想法很清楚……)
第一个箱线图的结果(正确绘制):
第二个(如前所述,绘制在第一个框上,每个框可以看到两条绿线,而不是一条线):
数据帧构建不是问题所在。我检查了一下,它们的值是正确的。你知道吗
经过大量的检查,我想问题出在图形定义上,但没有指出问题所在。你知道吗
如果要创建6个图形,可以使用for循环并在循环中创建每个图形。你知道吗
上面的代码为每个数据帧创建一个图形。如果你想把它们放到子图中,你可以使用下面的代码。你知道吗
如果有什么不清楚,请告诉我。你知道吗
有几种方法:
在每个
fig.savefig()
行之后添加plt.clf()
。除非清除绘图,否则程序将覆盖已经存在的任何绘图。plt.clf()
清除绘图并允许您重新开始下一个图像。你知道吗即:
看看这个例子:matplotlib examples
通过使用
plt.subplots()
,您可以创建3个不同的绘图子绘图,然后将它们保存为一个图像。当然,你可以为6个情节做类似的事情。你可以把
plt.figure()
放在每个新绘图的开头,这将从头开始创建一个图形。相关问题 更多 >
编程相关推荐