[PYTHON] A memorandum about matplotlib

This time, I would like to keep a memorandum about the knowledge I gained when displaying the stock price graph.

Development environment

MacOS Python3.7(anaconda) VSCode

How to use Matplotlib ④ (plt.subplots, plt.title, plt.legend) | Introduction to visualization with Python # 4 Basics of matplotlib drawing-for those who don't understand fig and axes GridSpec How to automate plot updates with Matplotlib (https://www.delftstack.com/ja/howto/matplotlib/how-to-automate-plot-updates-in-matplotlib/)

What I didn't understand with matplotlib

First, let's write about what we didn't understand when using matplotlib.pyplot.

Well, I had such a question.

Well, it seems that it is possible to display the graph by any method, but I think that the method will be different when it comes to making detailed settings.

What are ax and fig?

Fig is like a piece of paper for drawing graphs. I think everyone will write graphs on a piece of paper, and that paper will be this fig.

axes is a single square surrounded by thex and y linesof the graph.

About ax.plot (x, y)

I wanted to display a stock price graph, so I was able to get the value of y (stock price data), but I didn't know what to do with the value of x (time).

However, I was able to execute it by inserting ax.plot (stock price data). To be honest, I don't know why it was possible to execute it, but I think it was possible to execute it because the acquired data was also acquired with time data.

Other memorandum

figsize The default for figsize is(6.4, 4.8), and the unit is inches. You can change the unit from inches to pixels by 100 times.

plt.subplot plt.subplot specifies the number of rows, number of columns, ordinal. :) 100 5th of 10x10-> RCP = 10105

When you want to change the ratio of the display area of ​​multiple graphs

gridspec is convenient

When I wanted to display multiple graphs, I wanted to make the ratio of the displayed area of ​​each graph different.

from matplotlib import gridspec
gs = gridspec.GridSpec(Number of vertical divisions,Number of horizontal divisions, height_ratios=(Up,under), width_ratios=(left,right))
ax = [plt.subplot(gs[0, 0]), plt.subplot(gs[0, 1]), ...]

It will be written like this. By the way, if you want to divide it into two parts, ax = [plt.subplot (gs [0,0])], then ax = [plt.subplot (gs [1,0])] Become.

Next, although it is a little different from the above writing method, I will show an example of writing using fig and ax.

fig = plt.Figure()
gs = gridspec.GridSpec(2, 1, height_ratios=(4, 1))
ax[0] = fig.add_subplot(gs[0])
ax[1] = fig.add_subplot(gs[1])

In this way, the flow is to separate multiple graph areas.

gridspec_kw can also be used

There are about two more useful tools, but this time I would like to introduce gridspec_kw.

If you are interested, please see the reference article below. How to create different subplot sizes in Matplotlib (https://www.delftstack.com/ja/howto/matplotlib/how-to-make-different-subplot-sizes-in-matplotlib/)

fig, ax = plt.subplots(2, 1, 
                         gridspec_kw={
                                      'height_ratios':[4,1]
                                        })


With this feeling, you can adjust the height ratio with ** height_ratios ** and the left-right ratio with ** width_ratios ** in gridspec_kw.

How to update the graph

For example, if you want to display 10 graphs one by one, you need to update 10 times.

ax.clear()

You can use this to erase the previously displayed graph.

You can also use ** ax.axis ('off') ** to erase axes and more.

If you want to display two graphs and update them

For example, if you have a total of 20 graphs and want to display two graphs at a time, you also need to update this 10 times. Describe how to update the graph in this case.

fig, ax = plt.subplots(2, 1, 
                         gridspec_kw={
                                      'height_ratios':[4,1]
                                        })
ax[0].clear()
ax[1].clear()

As you can see, there are ax [0] and ax [1], so you need to delete both of them.

fig.canvas.draw () and fig.canvas.flush_events ()

These two processing statements are functions for redrawing the graph.

fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(0.1) # 0.Open for 1 second

By writing this statement, the process is to update the graph for the entire fig.

Summary

This time, I wrote the part of matplotlib that I don't understand personally. The article on matplotlib is posted by someone who is more detailed than me, so I think it will be easier to understand if you study there.

Recommended Posts

A memorandum about matplotlib
A memorandum about Nan.
Matplotlib memorandum
A memorandum about correlation [Python]
A memorandum about Python mock
Memorandum about validation
A memorandum of understanding about django's QueryDict
A note about __call__
A memorandum about the Python tesseract wrapper library
A note about subprocess
A note about mprotect (2)
matplotlib Graph-related memorandum (minimum)
A memorandum about the warning of the pylint output result
About python beginner's memorandum function
Memorandum @ Python OR Seminar: matplotlib
A small memorandum of openpyxl
A note about KornShell (ksh)
A note about TensorFlow Introduction
A memorandum regarding γ conversion
A little more about FIFO
A memorandum of using eigen3
A note about [python] __debug__
A story about installing matplotlib using pip with an error
A memorandum when using beautiful soup
A refreshing story about Python's Slice
A memorandum to change to Manjaro Linux
Python: A Note About Classes 1 "Abstract"
[Python] A memorandum of beautiful soup4
A note about get_scorer in sklearn
A sloppy story about Python's Slice
Draw a loose graph with matplotlib
I have a question about whitespace
Run Matplotlib on a Docker container
A memorandum of files under conf.d
A story about using Python's reduce
About the size of matplotlib points
Write a stacked histogram with matplotlib
A memorandum of closure survey contents
A note about mock (Python mock library)
[Memorandum] A story about trying OpenCV tutorial (face recognition) in a Windows environment
Memorandum / memo about programming learning / competitive programming site
A story about remodeling Lubuntu into a Chromebook
How to draw a graph using Matplotlib
Display matplotlib diagrams in a web application
A story that stumbled upon installing matplotlib
Memorandum about regression and binary classification metrics
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
Try drawing a normal distribution with matplotlib
Memorandum of python beginners About inclusion notation
A Java programmer studied Python. (About type)
A story about machine learning with Kyasuket
Make a partially zoomed figure with matplotlib
About Japanese fonts of matplotlib (for Mac)
[V11 ~] A memorandum to put in Misskey
A story about Python pop and append
Python Basic Memorandum Part 3-About Object Orientation-
Memo about Sphinx Part 1 (Creating a project)
A story about a 503 error on Heroku open
Make matplotlib look like a flat design
Run matplotlib on a Windows Docker container