python - ploting and save multiple functions in the same file matplotlib -
i want save 6 graphs in 1 page using matplotlib. graphs each call of a function , came code below test before saving it:
def save_plot (output_df, path = none): fig = plt.figure(1) sub1 = fig.add_subplot(321) plt.plot(plot_ba(output_df)) sub2 = fig.add_subplot(322) sub2.plot(plot_merchantablevol(output_df)) sub3 = fig.add_subplot(323) sub3.plot(plot_topheight(output_df)) sub4 = fig.add_subplot(324) sub4.plot(plot_grtotvol(output_df)) sub5 = fig.add_subplot(325) sub5.plot(plot_sc(output_df)) sub6 = fig.add_subplot(326) sub6.plot(plot_n(output_df)) plt.show()
the way is, create page 6 empty plots, create 6 separate plots every function call. plot_ba(output_df), example, function call read csv file , create plot (individually working). other similar functions , working well. seems missing put graphs in designated place of fig.
here 1 of functions using.
def plot_ba(output_df): ba = output_df.loc[:,['ba_aw','ba_sw', 'ba_sb','ba_pl']] baplot = ba.plot() plt.xlabel('year', fontsize=14) plt.ylabel('ba (m2)') return true
any tips?
try this:
import matplotlib.pyplot plt import random def function_to_call(func_name, ax): data = range(10) random_x_label = random.choice('abcdefghijk') random_y_label = random.choice('abcdefghijk') random.shuffle(data) # demonstration ax.plot(data) ax.set_xlabel(random_x_label) ax.set_ylabel(random_y_label) ax.set_title(func_name) fig = plt.figure(1) sub1 = fig.add_subplot(321) function_to_call('call_0', sub1) sub2 = fig.add_subplot(322) function_to_call('call_1', sub2) sub3 = fig.add_subplot(323) function_to_call('call_2', sub3) sub4 = fig.add_subplot(324) function_to_call('call_3', sub4) sub5 = fig.add_subplot(325) function_to_call('call_4', sub5) sub6 = fig.add_subplot(326) function_to_call('call_5', sub6) plt.tight_layout() # improve spacings plt.show() fig.savefig('output_plot.png')
the idea forward axes-object (which 1 of many inside whole figure) functions. need axes-level functions. notice difference between plt.xlabel
(figure-level) , ax.set_xlabel
(axes-level).
Comments
Post a Comment