[Matplotlib](http://matplotlib. We will focus on data visualization with org /). From this time, it will be a combination technique in combination with pandas.
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 ")
#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 ")
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
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 ")
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 ")
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 ")
It was very easy to visualize the CSV data.
Recommended Posts