Data visualization method using matplotlib (2)

Following on from Yesterday, I will explain the functions of matplotlib.

Colors, markers, linetypes

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

prop = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")

r = randn(30).cumsum()

#Specify color, linetype, marker
#Black, dashed line, marker is o
plt.plot(r, color='k', linestyle='dashed', marker='o')

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

image.png

Lines are connected by a straight line by default. You can change this with the drawstyle option.

#Explicit RGB value
plt.plot(r, color='#ff0000', linestyle='dashed', marker='o', label='dashed')
#Change drawstyle
plt.plot(r, color='#0000ff', drawstyle='steps-post', label='steps-post')
#Add a legend
plt.legend(loc='best')

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

image2.png

Scale, label

plt.xlim () and plt.xticks () return the current value when called with no arguments. Parameters can be set by specifying a value as an argument to this.

#Check the current value with no arguments
print( plt.xlim() )
# => (0.0, 30.0)
print( plt.xticks() )
# => (array([  0.,   5.,  10.,  15.,  20.,  25.,  30.]), <a list of 7 Text xticklabel objects>)

#Set a new value
plt.xlim([0, 40])
plt.xticks([0,4,8,12,16,20,24,28,32,36,40])

print( plt.ylim() )
# => (-7.0, 3.0)
print( plt.yticks() )
# => (array([-8., -6., -4., -2.,  0.,  2.,  4.]), <a list of 7 Text yticklabel objects>)

plt.ylim([-10, 10])
plt.yticks([-10,-8,-6,-4,-2,0,2,4,6,8,10])

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

image3.png

Axis customization

Consider the following random walk plot.

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

r = randn(1000).cumsum()
ax.plot(r)

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

image4.png

Let's customize the scale and label of this. For example, the X-axis is ranked by 250, the characters are tilted 30 degrees, and they are displayed in Japanese.

ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['A', 'B', 'C', 'D', 'E'], rotation=30, fontsize='small')
ax.set_title('Test matplotlib plot', fontproperties=prop)
ax.set_xlabel('Rank', fontproperties=prop)

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

image5.png

Add legend

An easy way to identify the plotted data is to label it and display it in a legend. It will be easier to understand if you separate the colors and line types for each data.

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

def randn1000():
    return randn(1000).cumsum()

ax.plot(randn1000(), 'k', label='one')
ax.plot(randn1000(), 'b--', label='two')
ax.plot(randn1000(), 'r.', label='three')
ax.plot(randn1000(), 'g+', label='four')
ax.plot(randn1000(), 'b*', label='five')

plt.ylim([-100, 100])

ax.legend(loc='best')

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

image6.png

Options when saving a file

You can specify image file options with plt.savefig ().

argument Description
fname The character string containing the file path, the Python file object, and the format are automatically determined from the extension.
dpi The number of dots per inch and the resolution of the figure. The default is 100.
facecolor,edgecolor Background color outside the subplot. The default is w(White) 。
format When you want to specify the file format explicitly. png,pdf etc.
bbox_inches Specify the part to save in the figure. Specifying tight removes the blank area around the figure.

This time around, pandas hasn't appeared yet, so so far we're talking purely about matplotlib. From the next time, I will plot in combination with pandas.

reference

Matplotlib usage notes http://www.geocities.jp/showa_yojyo/note/python-matplotlib.html

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