Plt,_seaborn

import csv
f=open('seoul.csv')
data=csv.reader(f)
header=next(data)
max_temp=-999
max_date=''
for row in data:
    if row[-1] == '' :
        row[-1] = -999
    row[-1] = float(row[-1])
    if max_temp < row[-1] : 
        max_date = row[0]
        max_temp = row[-1]
f.close()
print('기상 관측 이래 서울의 최고 기온이 가장 높았던 날은',max_date+'로',max_temp,'도 였습니다.')

기상 관측 이래 서울의 최고 기온이 가장 높았던 날은 2018-08-01로 39.6 도 였습니다.
import matplotlib.pyplot 
import matplotlib.pyplot as plt 
import matplotlib.pyplot as plt 
plt.plot([10,20,30,40])
plt.show()

png

import matplotlib.pyplot as plt 
plt.plot([1,2,3,4],[12,43,25,15])
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

import matplotlib.pyplot as plt 
plt.title('plotting')
plt.plot([10,20,30,40])
plt.show()

png

import matplotlib.pyplot as plt 
plt.title('legend')
plt.plot([10,20,30,40],label='asc')
plt.plot([40,30,20,10],label='desc')
plt.legend()
plt.show()

png

import matplotlib.pyplot as plt 
plt.title('color') # 제목 설정 
#그래프 그리기 
plt.plot([10,20,30,40],color='skyblue',label='asc')
plt.plot([40,30,20,10],color='pink',label='desc')
plt.legend()
plt.show()

png

import matplotlib.pyplot as plt 
plt.title('linestyle') # 제목 설정 
#빨간색 dashed 그래프
plt.plot([10,20,30,40],color='r',linestyle='--',label='dashed')
#초록색 dotted 그래프 
plt.plot([40,30,20,10],color='g',ls=':',label='dotted')
plt.legend()
plt.show()

png

import matplotlib.pyplot as plt 
plt.title('marker') # 제목 설정 
#빨간색 dashed 그래프
plt.plot([10,20,30,40],'r.',label='circle')
#초록색 삼각형 마커
plt.plot([40,30,20,10],'g^',label='triangle up')
plt.legend()
plt.show()

png

plt.plot(result,'r')
plt.figure(figsize=(10,2))
plt.show()

png

<Figure size 720x144 with 0 Axes>
s= 'hello pyhton'
print(s.split())
['hello', 'pyhton']
date='1907-10-01'
print(date.split('-'))
print(date.split('-')[0])
print(date.split('-')[1])
print(date.split('-')[2])
['1907', '10', '01']
1907
10
01
for row in data : 
    if row[-1] !='' :
        if row[0].split('-')[1]=='08' :
            result.append(float(row[-1]))
plt.plot(result,'hotpink')
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

for row in data : 
    if row[-1] !='' :
        if row[0].split('-')[1]=='02' and row[0].split('-')[2]=='14':
            result.append(float(row[-1]))
import csv
import matplotlib.pyplot as plt 

f=open('seoul.csv')
data=csv.reader(f)
next(data)
high= []
low= []

for row in data : 
    if row[-1]!='' and row[-2] !='':
        if 1983 <= int(row[0].split('-')[0]) : 
            if row[0].split('-')[1] == '02' and row[0].split('-')[2]=='14' :
                high.append(float(row[-1]))
                low.append(float(row[-2]))

plt.rc('font',family='Malgun Gothic')
plt.rcParams['axes.unicode_minus'] =False
plt.title('내 생일의 기온 변화 그래프')
plt.plot(high,'hotpink',label='high')
plt.plot(low,'skyblue',label='low')
plt.legend()
plt.show()


png

import matplotlib.pyplot as plt 
plt.hist([1,1,2,3,4,5,6,6,7,8,10])
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

import random 
print(random.randint(1,6))
5
import random 
dice=[]
for i in range(5) : 
    dice.append(random.randint(1,6))
print(dice)
[3, 5, 4, 3, 3]
import matplotlib.pyplot as plt 
plt.hist(dice,bins=6)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

import random 
dice=[]
for i in range(100) : 
    dice.append(random.randint(1,6))
print(dice)
[5, 4, 5, 3, 5, 6, 3, 5, 4, 5, 3, 3, 2, 1, 5, 4, 5, 4, 2, 3, 1, 5, 1, 5, 6, 2, 4, 4, 4, 6, 3, 3, 3, 4, 1, 1, 3, 5, 3, 5, 1, 4, 3, 5, 4, 5, 5, 2, 6, 3, 2, 1, 5, 3, 1, 2, 6, 1, 3, 3, 3, 4, 4, 6, 5, 2, 1, 1, 1, 5, 3, 5, 3, 5, 2, 6, 1, 2, 1, 2, 5, 1, 2, 2, 3, 2, 1, 2, 5, 2, 1, 3, 6, 1, 3, 1, 5, 2, 5, 1]
import matplotlib.pyplot as plt 
plt.hist(dice,bins=6)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

#기온 그래프를 히스토 그램으로 표현하기 

import csv 
import matplotlib.pyplot as plt 

f=open('seoul.csv')
data=csv.reader(f)
next(data)
result=[]

for row in data : 
    if row[-1] !='' : 
        result.append(float(row[-1]))

plt.hist(result,bins=100,color='r')
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

# sns 를 이용하여 기온 그래프를 히스토 그램으로 표현하기
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
seoul=pd.read_csv("G:/seoul.csv",engine='python')
seoul
날짜 지점 평균기온(℃) 최저기온(℃) 최고기온(℃)
0 1907-10-01 108 13.5 7.9 20.7
1 1907-10-02 108 16.2 7.9 22.0
2 1907-10-03 108 16.2 13.1 21.3
3 1907-10-04 108 16.5 11.2 22.0
4 1907-10-05 108 17.6 10.9 25.4
... ... ... ... ... ...
40216 2019-01-13 108 1.2 -3.0 7.6
40217 2019-01-14 108 1.4 -2.4 5.3
40218 2019-01-15 108 -1.7 -7.2 2.6
40219 2019-01-16 108 -5.2 -10.1 -1.1
40220 2019-01-17 108 -0.3 -3.2 4.0

40221 rows × 5 columns

sns.distplot(seoul["평균기온(℃)"])
E:\anaconda3\lib\site-packages\seaborn\distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)





<AxesSubplot:xlabel='평균기온(℃)', ylabel='Density'>

png

#8월 평균기온 

import csv 
import matplotlib.pyplot as plt 

f=open('seoul.csv')
data=csv.reader(f)
next(data)
aug=[]

for row in data : 
    month = row[0].split('-')[1] 
    if row[-1] !='' :
            if month =='08':
                aug.append(float(row[-1]))
                
plt.hist(aug,bins=100,color='r')
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>

png

#sns 이용 8월 평균 기온 
import datetime
print(seoul)
print(seoul.index)
print(seoul.columns)
#열 이름을 변경 

seoul.rename(columns={'날짜':'date', '지점':'point', '평균기온(℃)':'average','최저기온(℃)':'low','최고기온(℃)':'high'}, inplace=True)
print(seoul)

               날짜   지점  평균기온(℃)  최저기온(℃)  최고기온(℃)
0      1907-10-01  108     13.5      7.9     20.7
1      1907-10-02  108     16.2      7.9     22.0
2      1907-10-03  108     16.2     13.1     21.3
3      1907-10-04  108     16.5     11.2     22.0
4      1907-10-05  108     17.6     10.9     25.4
...           ...  ...      ...      ...      ...
40216  2019-01-13  108      1.2     -3.0      7.6
40217  2019-01-14  108      1.4     -2.4      5.3
40218  2019-01-15  108     -1.7     -7.2      2.6
40219  2019-01-16  108     -5.2    -10.1     -1.1
40220  2019-01-17  108     -0.3     -3.2      4.0

[40221 rows x 5 columns]
RangeIndex(start=0, stop=40221, step=1)
Index(['날짜', '지점', '평균기온(℃)', '최저기온(℃)', '최고기온(℃)'], dtype='object')
             date  point  average   low  high
0      1907-10-01    108     13.5   7.9  20.7
1      1907-10-02    108     16.2   7.9  22.0
2      1907-10-03    108     16.2  13.1  21.3
3      1907-10-04    108     16.5  11.2  22.0
4      1907-10-05    108     17.6  10.9  25.4
...           ...    ...      ...   ...   ...
40216  2019-01-13    108      1.2  -3.0   7.6
40217  2019-01-14    108      1.4  -2.4   5.3
40218  2019-01-15    108     -1.7  -7.2   2.6
40219  2019-01-16    108     -5.2 -10.1  -1.1
40220  2019-01-17    108     -0.3  -3.2   4.0

[40221 rows x 5 columns]
seoul['date']=pd.to_datetime(seoul['date'])
seoul['date']
0       1907-10-01
1       1907-10-02
2       1907-10-03
3       1907-10-04
4       1907-10-05
           ...    
40216   2019-01-13
40217   2019-01-14
40218   2019-01-15
40219   2019-01-16
40220   2019-01-17
Name: date, Length: 40221, dtype: datetime64[ns]
seoul['year'] = seoul['date'].dt.year 
seoul['month'] = seoul['date'].dt.month 
seoul['day'] = seoul['date'].dt.day 

seoul
date point average low high year month day
0 1907-10-01 108 13.5 7.9 20.7 1907 10 1
1 1907-10-02 108 16.2 7.9 22.0 1907 10 2
2 1907-10-03 108 16.2 13.1 21.3 1907 10 3
3 1907-10-04 108 16.5 11.2 22.0 1907 10 4
4 1907-10-05 108 17.6 10.9 25.4 1907 10 5
... ... ... ... ... ... ... ... ...
40216 2019-01-13 108 1.2 -3.0 7.6 2019 1 13
40217 2019-01-14 108 1.4 -2.4 5.3 2019 1 14
40218 2019-01-15 108 -1.7 -7.2 2.6 2019 1 15
40219 2019-01-16 108 -5.2 -10.1 -1.1 2019 1 16
40220 2019-01-17 108 -0.3 -3.2 4.0 2019 1 17

40221 rows × 8 columns

#8월 특정 월만 추출 
seoul_aug=seoul[seoul['month'] == 8]
seoul_aug

sns.distplot(seoul_aug["high"],kde=False,rug=True)
E:\anaconda3\lib\site-packages\seaborn\distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
E:\anaconda3\lib\site-packages\seaborn\distributions.py:2055: FutureWarning: The `axis` variable is no longer used and will be removed. Instead, assign variables directly to `x` or `y`.
  warnings.warn(msg, FutureWarning)





<AxesSubplot:xlabel='high'>

png

#1월과 8월데이터 추출 

seoul18=seoul[seoul['month'].isin([1,8])]

seoul18['month'].value_counts()

seoul18
date point average low high year month day
92 1908-01-01 108 -5.9 -9.1 -1.3 1908 1 1
93 1908-01-02 108 -7.7 -13.9 -3.0 1908 1 2
94 1908-01-03 108 -6.2 -10.8 -2.9 1908 1 3
95 1908-01-04 108 -7.5 -12.8 -1.7 1908 1 4
96 1908-01-05 108 -4.8 -11.3 -0.1 1908 1 5
... ... ... ... ... ... ... ... ...
40216 2019-01-13 108 1.2 -3.0 7.6 2019 1 13
40217 2019-01-14 108 1.4 -2.4 5.3 2019 1 14
40218 2019-01-15 108 -1.7 -7.2 2.6 2019 1 15
40219 2019-01-16 108 -5.2 -10.1 -1.1 2019 1 16
40220 2019-01-17 108 -0.3 -3.2 4.0 2019 1 17

6806 rows × 8 columns

#1월 8월 그래프 그리기 

sns.distplot(data=seoul18,x="high",hue="month")
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-132-f005587ed421> in <module>
      1 #1월 8월 그래프 그리기
      2 
----> 3 sns.distplot(data=seoul18,x="high",hue="month")


TypeError: distplot() got an unexpected keyword argument 'data'
sns.distplot(seoul["high"][seoul["month"]==1], kde=False,label='Jan') 
sns.distplot(seoul["high"][seoul["month"]==8], kde=False, color="r",label='Aug')
plt.legend()
plt.figure(figsize=(30,10))
plt.show()

png

<Figure size 2160x720 with 0 Axes>
import warnings 
!pip install warnings
ERROR: Could not find a version that satisfies the requirement warnings (from versions: none)
ERROR: No matching distribution found for warnings
import warnings
warnings.filterwarnings('ignore')
sns.distplot(seoul["high"][seoul["month"]==8], kde=False,label='Aug')
<AxesSubplot:xlabel='high'>

png

#기온데이터를 상자 그림으로 표현하기 

import matplotlib.pyplot as plt
import random 
result=[]
for i in range(13) :
    result.append(random.randint(1,1000))
print(sorted(result))

plt.boxplot(result)
plt.show
[53, 196, 219, 232, 335, 395, 409, 409, 602, 604, 833, 883, 967]





<function matplotlib.pyplot.show(close=None, block=None)>

png

#최대기온 그래프를 상자 으로 표현하기 

import csv 
import matplotlib.pyplot as plt 

f=open('seoul.csv')
data=csv.reader(f)
next(data)
result=[]

for row in data : 
    if row[-1] !='' : 
        result.append(float(row[-1]))

plt.boxplot(result)
plt.show()

png

sns.boxplot(x="month",y="high",data=seoul18)
<AxesSubplot:xlabel='month', ylabel='high'>

png

sns.boxplot(x="month",y="high",data=seoul)

<AxesSubplot:xlabel='month', ylabel='high'>

png

#8월 일별 기온 데이터를 상자 그림으로 그리기 
sns.boxplot(x=seoul["day"][seoul["month"]==8],y="high",data=seoul)
plt.figure(figsize=(15,4))
plt.show()

png

<Figure size 1080x288 with 0 Axes>
git add
  File "<ipython-input-1-3d50770d72e9>", line 1
    git add
        ^
SyntaxError: invalid syntax

업데이트:

댓글남기기