** What you can do by reading this article **
Matplotlib can draw super basic diagrams
After a while after you can make a figure with gnuplot, you will think "I want to draw a beautiful figure more easily" (* There are individual differences). One of the most famous tools that can be used free of charge to create diagrams for research presentations is matlotlib. You can draw a pretty beautiful figure (* personal opinion). In this article, I've summarized only the basics of how to draw diagrams in ready-to-use matplotlib.
** For more detailed usage, I tried to summarize how to use matplotlib of python Is very easy to understand and will be helpful. ** ** In particular, the object-oriented interface is convenient, so it would be nice if it could be used.
--Environment - macOS mojave 10.14.6 - Python 3.7.5
Let's draw a simple function using matplotlib here What is happening on the computer when drawing $ y = f (x) $ (Ignoring the accuracy of the terms for clarity), For each element $ x_i $ in the $ x $ pair (array) The element $ y_i $ of the set (array) of $ y $ is calculated and the set is plotted. The same is true for gnuplot. (Therefore, especially in logarithmic plots, the plot points may not be correct unless the plot points are taken in detail in the part where the change is sudden.)
In Python, you can create a pair $ x $ as follows. Think of it as creating an array of arithmetic progressions.
x = numpy.arange(-10, 10, 0.01)
The argument is
x = numpy.arange (minimum value of x, maximum value of x, tolerance)
is.
You can draw $ y = f (x) $ using this arithmetic progression (like).
Let's try drawing a parabola.
sample.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#This is necessary when writing double-byte characters (Japanese) in the code
#The following is something like magic
#Name numpy np
import numpy as np
#Similarly, matplotlib.Name pyplot plt
import matplotlib.pyplot as plt
#x = np.arange(Minimum value of x, maximum value of x, step)
x = np.arange(-10, 10, 0.1)
#Function to draw
y = x*x
#Draw with x on the horizontal axis and y on the vertical axis
plt.plot(x, y)
#plot display
plt.show()
Next, let's draw various options with frequently used options.
sample2.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.1)
y = x*x
#When specifying the drawing range (same for x-axis)
plt.ylim([0, 50])
#Title and axis label
plt.title("TITLE")
plt.xlabel("Xlabel")
plt.ylabel("Ylabel")
#grid display on/off (default is False)
plt.grid(True)
#Options such as label, line color, and thickness
plt.plot(x,y, label="legend", color="red", lw=3, ls="--")
#Show legend
plt.legend()
#plot display
plt.show()
Finally to save the diagram
plt.savefig("sample_figure.eps")
It is OK if you write such as.
The ones introduced here are just examples, and other options (line color, line type, legend position, etc.) should be googled to find what you need / favorite.
Recommended Posts