Graph drawing using matplotlib

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


part of plot

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


Labeling part

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 part

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()


Now that we have a function, make plot_graph.py smart

new_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

Graph drawing using matplotlib
Graph drawing method with matplotlib
matplotlib graph album
Try using matplotlib
How to draw a graph using Matplotlib
100 language processing knock-79 (using scikit-learn): precision-recall graph drawing
Graph drawing in python
Using graph drawing using Python's Matplotlib + Seaborn on Windows, a non-Python execution environment
I tried using matplotlib
Band graph with matplotlib
Try drawing contour plots using matplotlib in OpenFOAM
Real-time drawing with matplotlib
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
Try drawing a social graph using Twitter API v2
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Graph Excel data with matplotlib (2)
Data visualization method using matplotlib (1)
Real-time graph display by matplotlib
Data visualization method using matplotlib (2)
Graph drawing with IPython Notebook
Graph time series data in Python using pandas and matplotlib
Using graph drawing using Python's Matplotlib + Seaborn for interprocess communication on Windows, a non-Python execution environment
Japaneseize Matplotlib with Alpine using Docker
matplotlib: Insert comment on timeline graph
Data visualization method using matplotlib (+ pandas) (5)
Draw a loose graph with matplotlib
Note: Graph drawing termplotlib on terminal
Draw retention rate graph in Matplotlib
Data visualization method using matplotlib (+ pandas) (3)
Jupyter does not show matplotlib graph
Excel graph creation using python xlwings
Cases using pandas plot, cases using (pure) matplotlib plot
Notes on using matplotlib on the server
Data visualization method using matplotlib (+ pandas) (4)
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
[Python] Set the graph range with matplotlib
Flexible animation creation using animation.FuncAnimation of matplotlib
Try drawing a normal distribution with matplotlib
I tried drawing a line using turtle
Create a graph using the Sympy module
External display of matplotlib diagrams using tkinter
Display Matplotlib xy graph in PySimple GUI.
Behind the graph drawing algorithm and Networkx
matplotlib Write text to time series graph
Graph trigonometric functions with numpy and matplotlib
Draw a graph with PyQtGraph Part 1-Drawing
Create a graph with borders removed with matplotlib