2018년 6월 26일 화요일

[Python] Matplotlib


1. Plot Examples


1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import matplotlib.pyplot as plt
#plt.show()
#plt.savefig('a.png')

def line_plot():
 from numpy import sin
 from matplotlib import pyplot
 x = [x*0.1 for x in range(100)]
 y = sin(x)
 pyplot.plot(x, y)
 pyplot.show()

def bar_chart():
 from random import seed
 from random import randint
 from matplotlib import pyplot
 seed(1)
 x = ['red', 'green', 'blue']
 y = [randint(0, 100), randint(0, 100), randint(0, 100)]
 pyplot.bar(x, y)
 pyplot.show()

def histogram_plot():
 from numpy.random import seed
 from numpy.random import randn
 from matplotlib import pyplot
 seed(1)
 x = randn(1000)
 pyplot.hist(x) #, bins=100)
 pyplot.show()

def scatter_plot():
 from numpy.random import seed
 from numpy.random import randn
 from matplotlib import pyplot
 seed(1)
 x = 20 * randn(1000) + 100
 y = x + (10 * randn(1000) + 50)
 pyplot.scatter(x, y)
 pyplot.show()

def box_whisker_plot(): 
 from numpy.random import seed
 from numpy.random import randn
 from matplotlib import pyplot
 seed(1)
 x = [randn(1000), 5 * randn(1000), 10 * randn(1000)]
 pyplot.boxplot(x)
 pyplot.show()

def pandas_plot():
 import matplotlib.pyplot as plt
 import pandas
 url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
 names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
 data = pandas.read_csv(url, names=names)
 data.hist()
 plt.show()
 data.plot(kind='density', subplots=True, layout=(3,3), sharex=False)
 plt.show()
 data.plot(kind='box', subplots=True, layout=(3,3), sharex=False, sharey=False)
 plt.show() 
 data1 = data[data.columns[2:4]]
 data1.plot()
 plt.show()
 data1.plot(kind='density')
 plt.show()
 data1 = data.loc[:,'pres':'mass']
 data1.plot(kind='density')
 plt.show()

def pandas_random():
 import pandas as pd
 import numpy as np
 np.random.seed(5)
 df = pd.DataFrame(np.random.randint(100, size=(100, 6)), 
       columns=list('ABCDEF'), 
       index=['R{}'.format(i) for i in range(100)])
 print(df)
 print(df.head())
 print(df[['C','E']].head())
 print(df[df.columns[2:4]].head())
 print(df.loc[:,df.columns.isin(list('ACE'))].head())
 print(df.loc['R6':'R10','C':'E'])
 print(df.iloc[2:6,2:5])
 print(df.iloc[0:6,0:2].copy())
 print(pd.DataFrame(df,columns=['B','E']).head())
 print(df.drop(['A','C'],axis=1).head())

def time_series_line_plot():
 from pandas import Series
 from matplotlib import pyplot
 series = Series.from_csv('daily-minimum-temperatures-in-me.csv', header=0)
 print(series.head())
 #series = series.astype(float)
 series.plot()
 pyplot.show()
 
def time_series_group_line_plot():
 from pandas import DataFrame
 from pandas import TimeGrouper
 from pandas import Series
 from matplotlib import pyplot
 series = Series.from_csv('daily-minimum-temperatures-in-me.csv', header=0)
 groups = series.groupby(TimeGrouper('A'))
 years = DataFrame()
 for name, group in groups:
  years[name.year] = group.values
 years.plot(subplots=True, legend=False)
 pyplot.show()
 
if __name__ == "__main__":
 #line_plot()
 #bar_chart()
 #histogram_plot()
 #scatter_plot()
 #box_whisker_plot()
 #time_series_line_plot()
 #time_series_group_line_plot()
 #pandas_plot()
 pandas_random()



댓글 없음:

댓글 쓰기