This time, I would like to keep a memorandum about the knowledge I gained when displaying the stock price graph.
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/)
First, let's write about what we didn't understand when using matplotlib.pyplot
.
ax.plot
and plt.plot
but I don't know which oneWell, 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.
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 lines
of the graph.
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
.
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 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.
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
.
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.
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.
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
.
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