Forcibly draw something like a flowchart with Python, matplotlib

Introduction

I did a numerical calculation, which is my job. The program is an improvement of the Python one I had before, but it involves convergence calculations, and I've always wanted to create something like a simple flowchart to show the calculation procedure. So, I made a program to make "like a flowchart" in Python. I thought about it in various ways, but in the end, I couldn't come up with a good idea, and the content was just to let the computer do the same thing as handwriting by embedding Polygon and text in the code instead of handwriting with a ruler and pencil on graph paper. At first, I intended to use only flowcharts, but the same tone is cooler for related items such as variable explanations, so I also made a program to create a table. In essence, it is no different from a flowchart program. The content of the program is super primitive, but the output is black and white, but it may be quite cool (self-satisfaction). The programming environment is computer: MacBook Pro (Retina, 13-inch, Mid 2014), Python 3.5.2. Since I am working overseas, there is no particular need to output in Japanese, so please understand that the output is a poor English expression.

What are the benefits of creating something like this in Python? .. .. .. .. ..

Is it such a place? Eventually, I would like to devise a way to draw such a figure more efficiently.

programming

A part of the program body is shown below.

python


# Flowchart
from math import *
import sys
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.ticker import *

param=sys.argv
iflag=int(param[1]) # flag for axis drawing
# 0: without axis and grid, 1: with axis and grid

# drawing area: w x h = 16 x 25 imaged A4 paper
xmin=-8
xmax=8
ymin=0
ymax=25
dh=0.7 # height of one row
A=[]   # actural y-axis
B=[]   # grid number in y-axis
for i in range(0,int(ymax//dh)+1):
    s='{0:.1f}'.format(dh*i)
    A=A+[float(s)]
    B=B+[i]

fnameF='fig_flowchart.png'
fig = plt.figure()
ax1=plt.subplot(111)
ax1 = plt.gca()
ax1.set_xlim([xmin,xmax])
ax1.set_ylim([ymax,ymin])
aspect = (abs(ymax-ymin))/(abs(xmax-xmin))*abs(ax1.get_xlim()[1] - ax1.get_xlim()[0]) / abs(ax1.get_ylim()[1] - ax1.get_ylim()[0])
ax1.set_aspect(aspect)
if iflag==0: 
    plt.axis('off')
else:
    ax1.tick_params(labelsize=6)
    ax1.xaxis.set_major_locator(MultipleLocator(1))
    ax1.yaxis.set_major_locator(MultipleLocator(dh))
    plt.yticks(A,B)
    plt.grid(which='both',lw=0.3, color='#cccccc',linestyle='-')

#################################################
#                                               #
#     (something to draw)                       #
#                                               #
#################################################

plt.savefig(fnameF, dpi=200, bbox_inches="tight", pad_inches=0.2)
#plt.show()
Program Description
py_fig_flowchart.py Flowchart creation program
py_fig_table.py Table Creator

Deliverable product

The left side of the result diagram is the table creation program, and the right side is the one drawn by the flowchart creation program, which is combined with ImageMagick to make one image. The execution script is as follows.

a.txt


python py_fig_flowchart.py 0
python py_fig_table.py 0

convert -trim fig_flowchart.png -bordercolor 'white' -border 10x10 fig_flowchart.png
convert -trim fig_table.png -bordercolor 'white' -border 10x10 fig_table.png

montage -tile 2x1 -geometry 465x670 fig_table.png fig_flowchart.png fig_floodroutine.png

The deliverables are as follows. There is no such thing as "Is this flow an infinite loop?"

fig_floodroutine.png

that's all

Recommended Posts

Forcibly draw something like a flowchart with Python, matplotlib
Forcibly draw something like a flowchart with Python, matplotlib
[Python] How to draw a line graph with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
Draw a loose graph with matplotlib
Draw Lyapunov Fractal with Python, matplotlib
#I tried something like Vlookup with Python # 2
Draw a flat surface with a matplotlib 3d graph
[Python] Draw a directed graph with Dash Cytoscape
Try to draw a life curve with python
[Python] How to draw multiple graphs with Matplotlib
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Draw a graph with matplotlib from a csv file
[Python] How to draw a histogram in Matplotlib
Visualize grib2 on a map with python (matplotlib)
Heatmap with Python + matplotlib
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
[Python] Draw a Qiita tag relationship diagram with NetworkX
I tried to draw a route map with Python
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
[Python] How to create a 2D histogram with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Draw a graph with NetworkX
Make a fortune with Python
Draw a heart in Python
Create a directory with python
Draw a graph with networkx
Do something like a Python interpreter in Visual Studio Code
I made a puzzle game (like) with Tkinter in Python
Expand a Python nested dictionary to do something like Pandas' MultiIndex
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
Draw a graph with Julia + PyQtGraph (2)
[Road to Python Intermediate] Call a class instance like a function with __call__
[Python] font family and font with matplotlib
Let's make a GUI with python.
Something like JS setTimeout in python
Solve ABC166 A ~ D with Python
Draw Japanese with matplotlib on Ubuntu
Draw a scatterplot matrix in python
Create a virtual environment with Python!
I made a fortune with Python.
Draw a beautiful circle with numpy
Draw a graph with Julia + PyQtGraph (1)
Building a virtual environment with Python 3
Draw a graph with Julia + PyQtGraph (3)
Solve ABC168 A ~ C with Python
Draw a watercolor illusion with edge detection in Python3 and openCV3
Make a recommender system with python
Draw Koch curve with Python Turtle
Let's feel like a material researcher with python [Introduction to pymatgen]
Heatmap with Dendrogram in Python + matplotlib
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Draw a graph with pandas + XlsxWriter
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
Draw an illustration with Python + OpenCV
Easy to draw graphs with matplotlib