I use graph drawing a lot, so I personally created the minimum necessary. I'll omit the explanation of the functions in the basic matplotlib, as others have probably explained them. ʻImport The import of matplotlib.pyplot` is omitted.
plot_graph.py
def plot_graph(x, y=[], graph_name='graph.png', \
save_flag=True, show_flag=False, label_x='', label_y='', label='',title=''):
if label: pyplot.plot(x, y, label=label)
else: pyplot.plot(x, y)
if title: pyplot.title(title)
if label_x: pyplot.xlabel(label_x)
if label_y: pyplot.ylabel(label_y)
if save_flag: pyplot.savefig(graph_name)
if show_flag: pyplot.show()
As a default setting, if you give x, y
as an argument, it will be written to the graph.png
file.
x, y
is a sequence input.
As an option, I put the file name, save or not, changeable to show, each axis label, graph label, title.
I thought it would be better to separate save_flag
and show_flag
, but I thought it would be easier to understand when I saw it later.
Partially functionalized below
plot_only.py
def plot_only(x, y=[], label=''):
if label:
if y: pyplot.plot(x, y, label=label)
else: pyplot.plot(x, label=label)
else:
if y: pyplot.plot(x, y)
else: pyplot.plot(x)
Correspondence when there is a label and there is only one argument
add_label.py
def add_label(label_x='', label_y='', title=''):
if title: pyplot.title(title)
if label_x: pyplot.xlabel(label_x)
if label_y: pyplot.ylabel(label_y)
output_graph.py
def output_graph(save_flag=True, show_flag=False, graph_name='graph.png'):
if save_flag: pyplot.savefig(graph_name)
if show_flag: pyplot.show()
plot_graph.py
smartnew_plot_graph.py
def new_plot_graph(x, y=[], graph_name='graph.png', \
save_flag=True, show_flag=False, label_x='', label_y='', label='',title=''):
plot_only(x, y, label)
add_label(label_x, label_y, title)
output_graph(save_flag, show_flag, graph_name)
I don't use legend, display range, xtrick, etc. so much, so I didn't write it this time. Like this, it was a function I needed.
Recommended Posts