У меня есть код, который генерирует график и сохраняет график в виде файла .png
в текущем каталоге. По какой-то причине один график создается с текстом, который помещается на панель графика, а второй график (созданный с помощью того же кода) фактически увеличивает текстовые метки оси. Кто-нибудь может объяснить это поведение?
import pandas as pd
import matplotlib.pyplot as plt
import glob
import os
def get_basename(fp):
"Get the basename of filepath such that /PATH/TO/FILE.csv returns FILE"
return os.path.splitext(os.path.basename(fp))[0]
def get_model_type(fp, prefix):
"""All files represent models either 'linear' or 'quad'.
For instance all files are either linear_*_out.csv or quad_*_out.csv,
this function will filter a the list based on desired prefix"""
return filter(lambda x: get_basename(x).startswith(prefix), file_paths)
def read_assign(fp, col_name):
"""This function will read in the csv from input file path and assign
unique column identifier of the specific model. This will be important
when plotting the data"""
return pd.read_csv(fp).assign(model_id=col_name)
def produce_plot(df, plotname):
color_labels = df['model_id'].unique()
color_pal = ['black', 'red', 'cyan', 'brown', 'purple']
color_map = dict(zip(color_labels, color_pal))
grouped_sim = df.groupby('model_id')
for key, group in grouped_sim:
plt.plot(group['average_fuel_T'], group['avg_th_cond'],
label=key, color=color_map[key])
plt.ylabel('Thermal Conductivity', fontsize=20)
plt.xlabel('Temperature (K)', fontsize=20)
plt.legend(loc="upper left", prop=dict(weight='bold'))
plt.grid(b=True, which="major", axis="both", linestyle="-.")
plt.rc('xtick', labelsize=15)
plt.rc('ytick', labelsize=15)
plt.savefig(plotname)
plt.close()
working_dir = os.path.abspath('.')
file_paths = glob.glob(os.path.join(working_dir, '*_out.csv'))
linear_files = get_model_type(file_paths, 'linear')
linear_model_ids = [get_basename(file) for file in linear_files]
quad_files = get_model_type(file_paths, 'quad')
quad_model_ids = [get_basename(file) for file in quad_files]
raw_linear_dat = pd.concat(map(read_assign, linear_files, linear_model_ids))
raw_quad_dat = pd.concat(map(read_assign, quad_files, quad_model_ids))
produce_plot(raw_linear_dat, 'linear_temp_cond.png')
produce_plot(raw_quad_dat, 'quad_temp_cond.png')

