[PYTHON] Data visualization method using matplotlib (1)

Various previously introduced how to draw diagrams by matplotlib and pandas / ynakayama / items / 68eff3cb146181329b48) But I didn't mention the specific parameters. From this time, I will follow the drawing with matplotlib several times.

Diagram and subplot

The matplotlib Figure object provides plotting functionality. The plt.figure () method draws a new window with nothing drawn. The add_subplot () method creates a subplot inside it.

import numpy as np
from pandas import *
from pylab import *
import matplotlib.pyplot as plt
from matplotlib import font_manager
from numpy.random import randn

#Required to use Japanese
fontprop = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")

#Draw a new window
fig = plt.figure()

#Add subplot
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)

plt.show()
plt.savefig("image.png ")

image.png

Data drawing to subplot

When you plot, matplotlib draws on the last of the figures and subplots you use.

ran = randn(50).cumsum()
#Black(k)Draw with the dashed line of
plt.plot(ran, 'k--')

plt.show()
plt.savefig("image2.png ")

image2.png

You can draw on any subplot by explicitly calling the subplot instance method.

ax1.hist(rand(100), bins=20, color='k', alpha=0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3 * randn(30))

plt.show()
plt.savefig("image3.png ")

image3.png

Adjust the space

Use the subplots_adjust method to adjust the space between subplots.

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
    for j in range(2):
        axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)

#Try to fill in the blanks between the subplots
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0, hspace=0)

plt.show()
plt.savefig("image4.png ")

image4.png

reference

Introduction to data analysis with Python-Data processing using NumPy and pandas http://www.oreilly.co.jp/books/9784873116556/

Recommended Posts

Data visualization method using matplotlib (1)
Data visualization method using matplotlib (2)
Data visualization method using matplotlib (+ pandas) (5)
Data visualization method using matplotlib (+ pandas) (3)
Data visualization method using matplotlib (+ pandas) (4)
Python application: data visualization # 2: matplotlib
Implement "Data Visualization Design # 2" with matplotlib
Try using matplotlib
Visualization of latitude / longitude coordinate data (assuming meteorological data) using cartopy and matplotlib
Try using PHATE, a dimensionality reduction and visualization method for biological data
Implement "Data Visualization Design # 3" with pandas and matplotlib
Data analysis using xarray
Python Data Visualization Libraries
Graph drawing using matplotlib
Data cleansing 2 Data cleansing using DataFrame
I tried using matplotlib
Data cleaning using Python
Data visualization with pandas
Method call using __getattr__
I tried clustering ECG data using the K-Shape method
Visualization method of data by explanatory variable and objective variable
[Latest method] Visualization of time series data and extraction of frequent patterns using Pan-Matrix Profile
Instantly create a diagram of 2D data using python's matplotlib
Graph Excel data with matplotlib (1)
Try using matplotlib with PyCharm
Classify data by k-means method
Graph drawing method with matplotlib
Visualization of data by prefecture
Graph Excel data with matplotlib (2)
How to add new data (lines and plots) using matplotlib
Linear regression method using Numpy
Graph time series data in Python using pandas and matplotlib
SQL connection method using pyodbc
Data analysis using python pandas
Get Salesforce data using REST API
Data acquisition using python googlemap api
Visualization of mixed matrices using sklearn.metrics.ConfusionMatrixDisplay
Easy visualization using Python but PixieDust
Data visualization in Python-draw cool heatmaps
Versatile data plotting with pandas + matplotlib
Parsing CSV format data using SQL
Noise removal method using wavelet transform
Get Amazon data using Keep API # 1 Get data
Clustering and visualization using Python and CytoScape
Data acquisition memo using Backlog API
Easy data visualization with Python seaborn.
Recommendation of data analysis using MessagePack
Python application: data visualization part 1: basic
Get data from Twitter using Tweepy
Data analysis starting with python (data visualization 1)
Cases using pandas plot, cases using (pure) matplotlib plot
Notes on using matplotlib on the server
Data analysis starting with python (data visualization 2)
Create multiple line charts from a data frame at once using Matplotlib