** What you can do by reading this article **
Matplotlib can be used to create diagrams from serial number data files
This is a continuation of Introduction to drawing with matplotlib: Writing simple functions. Next, let's make a diagram from the data file of the calculation result that we did by ourselves. In the following, we will use an object-oriented interface.
--Environment - macOS mojave 10.14.6 - Python 3.7.5
Read the value from the data file of data000.dat ~ data099.dat
.
Suppose the contents of the data file are as follows.
data000.dat
# x_value y_value
1.000e+00 1.090e+03
1.010e+00 1.784e+03
1.020e+00 2.112e+03
...
It is OK if you turn the for statement to read the data files in order and draw them.
sample1.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(1.0, 20.0)
ax1.set_ylim(1000, 5000)
ax1.set_xlabel('x [cm]')
ax1.set_ylabel('y [g]')
#plot
filenum = 100
for i in range(filenum):
x, y = np.loadtxt("./data%02d.dat" % (i), comments='#', unpack=True)
ax1.plot(x, y, "-", color=cm.Reds(float(i+1)/filenum))
#Save
plt.savefig("sample1.eps")
x, y = np.loadtxt("./data%02d.dat" % (i), comments='#', unpack=True)
Read the i-th data file with
Store the values in the first column in x
and the values in the second column in y
.
To omit the first line (comment line) of the data file
comments ='#'
tells you that lines starting with #
are comment lines.
color = cm.Reds (float (i + 1) / filenum)
is an instruction to change the line color for each data.
Recommended Posts