카테고리 없음

2024.10.22 - 데이터 시각화 강의 정리

['팀애디'] 변서연 2024. 10. 22. 20:14
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [2,4,6,8,10]

plt.plot(x,y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example')
plt.show()

import pandas as pd
df = pd.DataFrame({
    'A' : [1,2,3,4,5],
    'B' : [5,4,3,2,1]
})
df

df.plot(x='A', y='B')
plt.show()

df.plot(x='A',y='B',color='pink', linestyle='--', marker='o')
plt.show

범례 추가하기
df.plot(x='A',y='B',color='red', linestyle='--', marker='o', label='Data series')
plt.show

ax=df.plot(x='A',y='B',color='pink', linestyle='--', marker='o')
ax.legend(['Data Series'])
plt.show

축, 제목 입력하기
ax=df.plot(x='A',y='B',color='pink', linestyle='--', marker='o')
ax.legend(['Data Series'])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Title')
ax.text(3,3, 'Some text', fontsize=12)
plt.show()

fig, ax=plt.subplots(figsize=(18,6))
ax=df.plot(x='A',y='B',color='pink', linestyle='--', marker='o')
ax.legend(['Data Series'])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Title')
ax.text(4,3, 'Some text', fontsize=12)
plt.show()
x = [1,2,3,4,5]
y = [2,4,6,8,10]
plt.plot(x,y)
plt.show()

plt.plot(data_grouped['year'], data_grouped['passengers'])
plt.xlabel('year')
plt.ylabel('passengers')
plt.show()

Bar vs histogram
df =pd.DataFrame({
    '도시': ['서울','부산','대구','인천'],
    '인구': [990, 250, 250, 290]
})
df
plt.bar(df['도시'], df['인구'])
plt.xlabel('도시')
plt.ylabel('인구')
plt.title('도시별 인구 수')
plt.show()

import numpy as np
data =np.random.randn(1000)
data.shape
plt.hist(data, bins=30)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

pie
size = [30,20,25,15,10]
labels = ['A','B','C','D','E']

plt.pie(size, labels=labels)
plt.title('Pie Chart')
plt.show

box plot
iris = sns.load_dataset("iris")
iris
species = iris['species'].unique()
sepal_lengths_list = [iris[iris['species'] == s]['sepal_length'].tolist() for s in species]
len(sepal_lengths_list)
plt.boxplot(sepal_lengths_list, labels=species)
plt.show()

sns.boxplot(x='species', y='sepal_length', data=iris)
plt.show()

scatter
plt.scatter(iris['sepal_length'], iris['sepal_width'])
plt.xlabel('Petal length')
plt.ylabel('Petal width')
plt.show