[PYTHON] Getting Started with Drawing with matplotlib: Creating Diagrams from Data Files

** 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

Getting Started with Drawing with matplotlib: Creating Diagrams from Data Files
Getting Started with Drawing with matplotlib: Writing Simple Functions
Getting started with Dynamo from Python boto
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting started with apache2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Real-time drawing with matplotlib
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Graph Excel data with matplotlib (1)
Getting Started with Go Assembly
Getting Started with PKI with Golang ―― 4
Getting Started with Python Django (1)
Graph drawing method with matplotlib
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Graph Excel data with matplotlib (2)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
4 Techniques for Creating Diagrams for Papers and Presentation Materials with matplotlib
Python3 | Getting Started with numpy
Getting Started with Flask # 2: Displaying Data Frames in Style Sheets
Getting Started with Python Django (5)
Getting Started with Poetry From installation to execution and version control
[AWS] Search and acquire necessary data from S3 files with S3 Select
Data engineers learn DevOps with a view to MLOps. ① Getting started
From "drawing" to "writing" the configuration diagram: Try drawing the AWS configuration diagram with Diagrams
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Versatile data plotting with pandas + matplotlib
Getting Started with Python Basics of Python
Install the data files with setup.py
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Implement "Data Visualization Design # 2" with matplotlib
Getting Started with CPU Steal Time
Receive textual data from mysql with python
Getting Started with python3 # 1 Learn Basic Knowledge
Play audio files from Python with interrupts
Getting Started with Flask with Azure Web Apps