pandas 箱线图包含之前保存的绘图内容

pandas boxplot contains content of plot saved before

我正在将数据名的一些列绘制到箱线图中。到目前为止,没问题。如下所示,我写了一些东西并且它有效。但是:第二个情节也包含第一个情节的情节。因此,正如您所见,我尝试使用“= None”或“del value”,但它不起作用。将 plot 函数放在外面也不能解决问题。

我的代码有什么问题?

这是一个可执行的例子

 import pandas as pd 
 
 d1 = {'ff_opt_time': [10, 20, 11, 5, 15 , 13, 19, 25  ], 'ff_count_opt': [30, 40, 45, 29, 35,38,32,41]}
 df1 = pd.DataFrame(data=d1)
 d2 = {'ff_opt_time': [1, 2, 1, 5, 1 , 1, 4, 5  ], 'ff_count_opt': [3, 4, 4, 9, 5,3, 2,4]}
 df2 = pd.DataFrame(data=d2)
 
 def evaluate2(df1, df2):
    
     def plot(df, output ):
         boxplot = df.boxplot(rot=45,fontsize=5)
         fig = boxplot.get_figure()
         fig.savefig(output + ".pdf")
     
     df_ot = pd.DataFrame(columns=['opt_time1' , 'opt_time2'])
     df_ot['opt_time1'] = df1['ff_opt_time']
     df_ot['opt_time2'] = df2['ff_opt_time']
     plot(df_ot, "bp_opt_time")
 
     df_op = pd.DataFrame(columns=['count_opt1' , 'count_opt2'])
     df_op['count_opt1'] = df1['ff_count_opt']
     df_op['count_opt2'] = df2['ff_count_opt']
     plot(df_op, "bp_count_opt_perm")    
 
 evaluate2(df1, df2)

这是另一个可执行示例。我什至使用了其他变量名。

import pandas as pd 

d1 = {'ff_opt_time': [10, 20, 11, 5, 15 , 13, 19, 25  ], 'ff_count_opt': [30, 40, 45, 29, 35,38,32,41]}
df1 = pd.DataFrame(data=d1)
d2 = {'ff_opt_time': [1, 2, 1, 5, 1 , 1, 4, 5  ], 'ff_count_opt': [3, 4, 4, 9, 5,3, 2,4]}
df2 = pd.DataFrame(data=d2)

def evaluate2(df1, df2):

    df_ot = pd.DataFrame(columns=['opt_time1' , 'opt_time2'])
    df_ot['opt_time1'] = df1['ff_opt_time']
    df_ot['opt_time2'] = df2['ff_opt_time']
    boxplot1 = df_ot.boxplot(rot=45,fontsize=5)
    fig1 = boxplot1.get_figure()
    fig1.savefig( "bp_opt_time.pdf")

    df_op = pd.DataFrame(columns=['count_opt1' , 'count_opt2'])
    df_op['count_opt1'] = df1['ff_count_opt']
    df_op['count_opt2'] = df2['ff_count_opt']
    boxplot2 = df_op.boxplot(rot=45,fontsize=5)
    fig2 = boxplot2.get_figure()
    fig2.savefig( "bp_count_opt_perm.pdf")

evaluate2(df1, df2)

我可以从您的代码中看出箱线图:boxplot1boxplot2 在同一张图中。你需要做的是指示将有两个地块。

这可以通过

  1. 使用 matplotlib 中的 pyplot 创建两个子图,此代码实现了 fig1, ax1 = plt.subplots() 的技巧,其中 ax1 指定要放入该轴的箱线图和 fig2 指定箱线图
  2. 解散 evaluate2 函数并在 jupyter notebook 的不同单元格中分别执行箱线图

解决方案 1:使用 pyplot

的两个子图
import pandas as pd 
import matplotlib.pyplot as plt

d1 = {'ff_opt_time': [10, 20, 11, 5, 15 , 13, 19, 25  ], 'ff_count_opt': [30, 40, 45, 29, 35,38,32,41]}
df1 = pd.DataFrame(data=d1)
d2 = {'ff_opt_time': [1, 2, 1, 5, 1 , 1, 4, 5  ], 'ff_count_opt': [3, 4, 4, 9, 5,3, 2,4]}
df2 = pd.DataFrame(data=d2)

def evaluate2(df1, df2):
    df_ot = pd.DataFrame(columns=['opt_time1' , 'opt_time2'])
    df_ot['opt_time1'] = df1['ff_opt_time']
    df_ot['opt_time2'] = df2['ff_opt_time']
    fig1, ax1 = plt.subplots() 
    boxplot1 = df_ot.boxplot(rot=45,fontsize=5)
    ax1=boxplot1
    fig1 = boxplot1.get_figure()
    fig1.savefig( "bp_opt_time.pdf")

    df_op = pd.DataFrame(columns=['count_opt1' , 'count_opt2'])
    df_op['count_opt1'] = df1['ff_count_opt']
    df_op['count_opt2'] = df2['ff_count_opt']
    fig2, ax2 = plt.subplots()
    boxplot2 = df_op.boxplot(rot=45,fontsize=5)
    fig2 = boxplot2.get_figure()
    ax2=boxplot2
    fig2.savefig( "bp_count_opt_perm.pdf")
    plt.show()
evaluate2(df1, df2)

解决方案 2:在不同的单元格中执行箱线图

根据评论更新:清除地块

清除剧情的两种方法,

  • 使用clf()绘制自己 matplotlib.pyplot.clf() 清除当前图形状态而不关闭它的函数

  • 使用cla()清除轴 matplotlib.pyplot.cla() 函数在不关闭 Axes 的情况下清除当前 Axes 状态。

调用fig.save

后调用plt.clf()函数即可

阅读this documentation on how to clear a plot in Python using matplotlib

只需从 Archana David 获取代码并将其放入您的绘图函数中:目标是调用“fig, ax = plt.subplots()”来创建一个新图形。

import pandas as pd
import matplotlib.pyplot as plt
d1 = {'ff_opt_time': [10, 20, 11, 5, 15, 13, 19, 25],
    'ff_count_opt': [30, 40, 45, 29, 35, 38, 32, 41]}
df1 = pd.DataFrame(data=d1)
d2 = {'ff_opt_time': [1, 2, 1, 5, 1, 1, 4, 5],
    'ff_count_opt': [3, 4, 4, 9, 5, 3, 2, 4]}
df2 = pd.DataFrame(data=d2)
def evaluate2(df1, df2):
    def plot(df, output):
    fig, ax = plt.subplots()
    boxplot = df.boxplot(rot=45, fontsize=5)
    ax = boxplot
    fig = boxplot.get_figure()
    fig.savefig(output + ".pdf")

    df_ot = pd.DataFrame(columns=['opt_time1', 'opt_time2'])
    df_ot['opt_time1'] = df1['ff_opt_time']
    df_ot['opt_time2'] = df2['ff_opt_time']
    plot(df_ot, "bp_opt_time")

    df_op = pd.DataFrame(columns=['count_opt1' , 'count_opt2'])
    df_op['count_opt1'] = df1['ff_count_opt']
    df_op['count_opt2'] = df2['ff_count_opt']
    plot(df_op, "bp_count_opt_perm")    

evaluate2(df1, df2)