[PYTHON] Getting Started with Drawing with matplotlib: Writing Simple Functions

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

sample1.png

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

sample2.png

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

Getting Started with Drawing with matplotlib: Writing Simple Functions
Getting Started with Python Functions
Getting Started with Drawing with matplotlib: Creating Diagrams from Data Files
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 Tkinter 2: Buttons
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)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
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
Getting Started with Python Basics of Python
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
Getting Started with CPU Steal Time
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Flask with Azure Web Apps
Getting Started with Python Web Scraping Practice
Try drawing a normal distribution with matplotlib
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Graph trigonometric functions with numpy and matplotlib
Getting Started with Lisp for Pythonista: Supplement
Getting Started with Heroku, Deploying Flask App
Getting Started with TDD with Cyber-dojo at MobPro
Getting started with Python with 100 knocks on language processing
Drawing tips with matplotlib on the server side
MongoDB Basics: Getting Started with CRUD in JAVA
Getting started with Keras Sequential model Japanese translation