[PYTHON] Add information to the bottom of the figure with Matplotlib

Return to list

You may want to add text information around the figure. You can add character information with plt.text, but it is troublesome because you have to specify the location one by one. The following code is added by using legend without specifying the location.

image.png

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
%matplotlib inline
plt.rcParams['font.size'] = 15

def r2(y1, y2):
    r2 = str(np.round(np.corrcoef(y1, y2)[0,1],3))
    return r2

xa_train = [1,3,5,7]
xa_test  = [2,4,6,9]
ya_train = xa_train + np.random.randn(4)
ya_test  = xa_test  + np.random.randn(4)

xb_train = [1,3,5,7]
xb_test  = [2,4,6,9]
yb_train = xb_train + np.random.randn(4)
yb_test  = xb_test  + np.random.randn(4)

plt.figure()
plt.subplots_adjust(wspace=0.2, hspace=0.4)

gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], height_ratios=[4,1])

plt.subplot(gs[0])
plt.scatter(xa_train, ya_train, color='k', label='train')
plt.scatter(xa_test, ya_test, color='r', label='test')
plt.xlim(0,10)
plt.ylim(0,10)
plt.xticks([0,2,4,6,8,10])
plt.yticks([0,2,4,6,8,10])
plt.plot([0,10],[0,10], color='gray', lw=0.5)
plt.grid()
plt.title('train')
plt.xlabel('measured')
plt.ylabel('predicted')

plt.subplot(gs[1])
plt.scatter(xb_train, yb_train, color='k',label='train')
plt.scatter(xb_test, yb_test, color='r', label='test')
plt.xlim(0,10)
plt.ylim(0,10)
plt.xticks([0,2,4,6,8,10])
plt.yticks([0,2,4,6,8,10])
plt.plot([0,0],[10,10])
plt.plot([0,10],[0,10], color='gray', lw=0.5)
plt.grid()
plt.title('test')
plt.xlabel('measured')
plt.tick_params(left=False,labelleft=False)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)

plt.subplot(gs[2])
plt.tick_params(left=False, labelleft=False,bottom=False, labelbottom=False)
for i in ['top','bottom','left','right'] : plt.gca().spines[i].set_visible(False)
plt.scatter(0,0,label='$R^2_{train}=$'+r2(xa_train,ya_train)+'\n$R^2_{test}=$'+r2(xa_test,ya_test),alpha=0)
plt.legend(frameon=False, loc='upper left')

plt.subplot(gs[3])
plt.tick_params(left=False, labelleft=False,bottom=False, labelbottom=False)
for i in ['top','bottom','left','right'] : plt.gca().spines[i].set_visible(False)
plt.scatter(0,0,label='$R^2_{train}=$'+r2(xb_train,yb_train)+'\n$R^2_{test}=$'+r2(xb_test,yb_test),alpha=0)
plt.legend(frameon=False, loc='upper left')

plt.show()

Recommended Posts

Add information to the bottom of the figure with Matplotlib
Match the colorbar to the figure with matplotlib
Align the size of the colorbar with matplotlib
PhytoMine-I tried to get the genetic information of plants with Python
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
I tried to get the movie information of TMDb API with Python
Increase the font size of the graph with matplotlib
The basis of graph theory with matplotlib animation
Visualize the behavior of the sorting algorithm with matplotlib
Add the attribute of the object of the class with the for statement
Just add the driver to the shape key with blender
Try to get the contents of Word with Golang
I tried to visualize the spacha information of VTuber
[Python] Summary of how to specify the color of the figure
[Introduction to Python] Basic usage of the library matplotlib
Extract the band information of raster data with python
Adjust the ratio of multiple figures with the matplotlib gridspec
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
I tried to visualize the characteristics of new coronavirus infected person information with wordcloud
The road to download Matplotlib
Change the style of matplotlib
I tried to find the entropy of the image with python
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
[Python] Read the csv file and display the figure with matplotlib
[pyqtgraph] Add region to the graph and link it with the graph region
I want to get the operation information of yahoo route
Settings to debug the contents of the library with VS Code
Reformat the timeline of the pandas time series plot with matplotlib
Try to automate the operation of network devices with Python
Let's visualize the number of people infected with coronavirus with matplotlib
The usual way to add a Kernel with Jupyter Notebook
I wrote the basic operation of matplotlib with Jupyter Lab
Get the source of the page to load infinitely with python.
Try to extract the features of the sensor data with CNN
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
Try to import to the database by manipulating ShapeFile of national land numerical information with Python
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
The story of not being able to run pygame with pycharm
Save the results of crawling with Scrapy to the Google Data Store
[Hugo] Summary of how to add pages to sites built with Learn
Add fields to features with ArcPy
Become familiar with (want to be) around the pipeline of spaCy
I tried to automate the watering of the planter with Raspberry Pi
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
Add cumulative ratio to matplotlib histogram
Change the Y-axis scale of Matplotlib to exponential notation (10 Nth power notation)
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
Try to solve the N Queens problem with SA of PyQUBO
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Consider the speed of processing to shift the image buffer with numpy.ndarray
Solving the Maze with Python-Supplement to Chapter 6 of the Algorithm Quick Reference-
How to monitor the execution status of sqlldr with the pv command
Paste the matplotlib figure on excel
[Python] I tried to visualize the prize money of "ONE PIECE" over 100 million characters with matplotlib.
I tried to expand the size of the logical volume with LVM