[PYTHON] Data visualization method using matplotlib (+ pandas) (3)

[Matplotlib](http://matplotlib. We will focus on data visualization with org /). From this time, it will be a combination technique in combination with pandas.

Line graph

When plotting objects in a series or data frame, it defaults to a line chart.

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

#Series simple plotting
s = Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10))
s.plot()

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

image.png

#Simple plotting of data frames
df = DataFrame(np.random.randn(10, 4).cumsum(0),
               columns=['A','B','C','D'],
               index=np.arange(0, 100, 10))
df.plot()
plt.show()
plt.savefig("image2.png ")

image2.png

Most of the methods for plotting in pandas can optionally specify a matplotlib subplot object for the ax parameter.

For a list of options you can specify for plot, you may want to refer to the official documentation below.

pandas.DataFrame.plot http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.plot.html

bar graph

The most commonly used is kind, which allows you to specify a linetype. If kind ='bar', it will be a bar graph.

#Visualize the series
data = Series(np.random.randn(16), index=list('abcdefghijklmnop'))
#Vertical bar graph
data.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
#Horizontal bar graph
data.plot(kind='barh', ax=axes[1], color='r', alpha=0.6)

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

image3.png

If you make the data frame a bar chart, the values in each row are grouped together.

#Visualize the data frame
df = DataFrame(np.random.randn(6, 4),
               index=['1','2','3','4','5','6'],
               columns=Index(['A','B','C','D'], name='Genus'))

print( df )
# =>
# Genus         A         B         C         D
# 1     -0.350817 -0.017378 -0.991230 -0.223608
# 2      0.478712 -0.472764  0.677484 -0.852312
# 3      1.402219  0.381440  0.370080  0.682125
# 4     -1.733590  0.296124 -0.014841  1.140705
# 5      0.373399  1.150718  1.341984  1.040759
# 6     -0.013301 -0.202793 -1.367493 -0.572954

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

df.plot(kind='bar') #Make a bar graph
plt.show(grid=False, alpha=0.8)
plt.savefig("image5.png ")

df.plot(kind='barh', stacked=True, alpha=0.5) #Make a stacked bar graph(stacked option)
plt.show()
plt.savefig("image6.png ")


image4.png

image5.png

image6.png

Visualize financial data

Previous [Analysis of financial data and its visualization](http://qiita.com/ynakayama/items/32b1ca2a108876f889cc Try plotting with pandas + matplotlib using csv used in).

df = read_csv('stock_px.csv') #Read CSV

print( df.head(10) ) #The beginning of the data frame
# =>
# [6 rows x 4 columns]
#             Unnamed: 0  AAPL   MSFT    XOM     SPX
# 0  2003-01-02 00:00:00  7.40  21.11  29.22  909.03
# 1  2003-01-03 00:00:00  7.45  21.14  29.24  908.59
# 2  2003-01-06 00:00:00  7.45  21.52  29.96  929.01
# 3  2003-01-07 00:00:00  7.43  21.93  28.95  922.93
# 4  2003-01-08 00:00:00  7.28  21.31  28.83  909.93
# 5  2003-01-09 00:00:00  7.34  21.93  29.44  927.57
# 6  2003-01-10 00:00:00  7.36  21.97  29.03  927.57
# 7  2003-01-13 00:00:00  7.32  22.16  28.91  926.26
# 8  2003-01-14 00:00:00  7.30  22.39  29.17  931.66
# 9  2003-01-15 00:00:00  7.22  22.11  28.77  918.22

df.plot()
plt.show()
plt.savefig("image7.png ")

image7.png

It was very easy to visualize the CSV data.

Recommended Posts

Data visualization method using matplotlib (+ pandas) (5)
Data visualization method using matplotlib (+ pandas) (3)
Data visualization method using matplotlib (+ pandas) (4)
Data visualization method using matplotlib (1)
Data visualization method using matplotlib (2)
Data visualization with pandas
Implement "Data Visualization Design # 3" with pandas and matplotlib
Python application: data visualization # 2: matplotlib
Data analysis using python pandas
Versatile data plotting with pandas + matplotlib
Graph time series data in Python using pandas and matplotlib
Cases using pandas plot, cases using (pure) matplotlib plot
Implement "Data Visualization Design # 2" with matplotlib
Read pandas data
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
Try using matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
[Pandas] Basics of processing date data using dt
100 language processing knock-20 (using pandas): reading JSON data
100 language processing knock-99 (using pandas): visualization by t-SNE
Analysis of financial data by pandas and its visualization (2)
Get Amazon RDS (PostgreSQL) data using SQL with pandas
Python Data Visualization Libraries
Analysis of financial data by pandas and its visualization (1)
Data analysis using Python 0
[Latest method] Visualization of time series data and extraction of frequent patterns using Pan-Matrix Profile
Data cleansing 2 Data cleansing using DataFrame
Data cleaning using Python
I tried clustering ECG data using the K-Shape method
[Python] Summary of table creation method using DataFrame (pandas)
Data manipulation with Pandas!
Aggregate event data into one-user, one-line format using pandas
Process csv data with python (count processing using pandas)
Shuffle data with pandas
Visualization method of data by explanatory variable and objective variable
[Memo] Text matching in pandas data frame using flashtext
Method call using __getattr__
[Numpy / pandas / matplotlib Exercise 01]
Instantly create a diagram of 2D data using python's matplotlib
How to add new data (lines and plots) using matplotlib
Easy-to-understand [Pandas] practice / data confirmation method for high school graduates
Analyze stock prices using pandas data aggregation and group operations
[Python] Random data extraction / combination from DataFrame using random and pandas
pandas Matplotlib Summary by usage
Graph Excel data with matplotlib (1)
Draw multiple graphs using Pandas
Try using matplotlib with PyCharm
Select features using text data
Classify data by k-means method
Visualization of data by prefecture
Graph Excel data with matplotlib (2)
Linear regression method using Numpy
Visualization memo by pandas, seaborn
Behavior of pandas rolling () method
SQL connection method using pyodbc
Data processing tips with Pandas