공식 Documentation

https://matplotlib.org/stable/plot_types/basic/index.html

 

Pairwise data — Matplotlib 3.8.2 documentation

Pairwise data Plots of pairwise \((x, y)\), tabular \((var\_0, \cdots, var\_n)\), and functional \(f(x)=y\) data.

matplotlib.org

 

0. 목적

 

본 포스팅의 목적은 Matplotlib의 위 documentation에서 설명하는 가장 기본적인 시각화 툴을 보다 자세히 이해하고자 한다.

 

1. plot(x,y)

예제 코드

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# make data
x = np.linspace(0, 10, 100)
y = 4 + 2 * np.sin(2 * x)

# plot
fig, ax = plt.subplots()

ax.plot(x, y, linewidth=2.0)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()

 

결과

 

코드 해석

필요한 라이브러리들 호출

import matplotlib.pyplot as plt
import numpy as np

 

plot 스타일 정의

plt.style.use('_mpl-gallery')

해당 스타일 외에도

plt.style.use('bmh')
plt.style.use('ggplot')
plt.style.use('classic')
plt.style.use('Solarize_Light2')
plt.style.use('default')

와 같이 다양한 스타일 사용 가능

 

예시

bmh
ggplot
classic

 

 

관련 공식 문서

https://matplotlib.org/stable/api/style_api.html

 

matplotlib.style — Matplotlib 3.8.2 documentation

matplotlib.style Styles are predefined sets of rcParams that define the visual appearance of a plot. Customizing Matplotlib with style sheets and rcParams describes the mechanism and usage of styles. The Style sheets reference gives an overview of the buil

matplotlib.org

 

데이터 생성

x = np.linspace(0, 10, 100)
y = 4 + 2 * np.sin(2 * x)

 

np.linspace에 들어가는 세 param은 각각 구간 시작점, 구간 끝점, 구간 내 숫자 개수 을 의미

 

예시

import numpy as np

x = np.linspace(1,10,100)

x

 

출력 결과

 

1부터 10까지, 처음을 1 마지막을 10으로 하는 100개 element를 가지는 1차원의 array를 만듦.

 

y는 해당 x에 대응되는 각각의 함수 값을 갖게 됨.

x는 numpy.ndarray의 타입을 갖게 되므로 위와 같은 연산이 가능

(list의 경우 해당 연산이 불가하는 것으로 아는데 y식 정의에 np가 들어가있어 가능한 듯)

 

 

 

fig, ax = plt.subplots()

 

관련 문서

https://towardsdatascience.com/clearing-the-confusion-once-and-for-all-fig-ax-plt-subplots-b122bb7783ca

 

Clearing the confusion once and for all: fig, ax = plt.subplots()

Learn about figure and axes objects in Matplotlib

towardsdatascience.com

 

이 부분은 많은 예시 코드들에서 공통적으로 사용하고 있는 부분인데 어떤 의미일까.

(Note : subplot과 subplots는 다름)

 

#기본
plt.subplots(nrows, ncols)

 

예시 1

fig, ax = plt.subplots()
plt.show()

 

예시 2

fig, ax = plt.subplots(2,1)
plt.show()

 

nrows와 ncols에 m, n이라는 정수형 인자를 각가 넣으면 m by n matrix 느낌의 plot들이 구성된다.

 

예시 3

sub_plots = plt.subplots(3,2)
print(sub_plots)
print("TYPE IS : ", type(sub_plots))

이 부분에서 살짝 의외였다. sub_plots 객체의 type이 tuple이라니.

아마 fig, ax로 쪼개는 것을 통해 length가 2짜리인 (fig, ax)의 형태로 이루어진 tuple이 아니었을까

 

예시 4

fig, ax = plt.subplots(3,2)
print(fig)
print(ax)
print("TYPE IS : ", type(fig))
print("TYPE IS : ", type(ax))

 

예시 5

sub_plots = plt.subplots(3,2)
print(sub_plots[0])
print("##########")
print(sub_plots[1])
print("##########")
print(sub_plots[1][0])

확인을 해보니 fig, ax의 길이가 2짜리인 튜플이 맞는 것 같다.

ax 파트에는 3 by 2 형태의 2차원 array들이 들어있어 각각에 대한 정의를 해줄 수 있다.

'Data > Visualization' 카테고리의 다른 글

[Matplotlib] 3D Plot  (0) 2023.12.08

 

from mpl_toolkits.mplot3d.axes3d import get_test_data

X, Y, Z = get_test_data(0.5)

print(f"X.shape={X.shape}")
print(f"Y.shape={Y.shape}")
print(f"Z.shape={Z.shape}")

 

출력 결과

X.shape=(12, 12)
Y.shape=(12, 12)
Z.shape=(12, 12)

 

 

import matplotlib.pyplot as plt

fig, axs = plt.subplots(ncols=3, figsize=(10, 4), subplot_kw={"projection":"3d"}, constrained_layout=True)

for ax, d in zip(axs, [1, 0.5, 0.1]):
    X, Y, Z = get_test_data(d)
    dim = X.shape[0]
    ax.plot_wireframe(X, Y, Z)
    ax.set_title(f"get_test_data({d}): {dim}x{dim}", fontsize="x-large", color="gray", fontweight="bold")

 

 

 

 

 

 

 

 

 

 

 

 

 

참고

 

https://matplotlib.org/stable/plot_types/3D/wire3d_simple.html#sphx-glr-plot-types-3d-wire3d-simple-py

 

plot_wireframe(X, Y, Z) — Matplotlib 3.8.2 documentation

plot_wireframe(X, Y, Z) See plot_wireframe. import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d plt.style.use('_mpl-gallery') # Make data X, Y, Z = axes3d.get_test_data(0.05) # Plot fig, ax = plt.subplots(subplot_kw={"projection": "3d"}

matplotlib.org

 

https://jehyunlee.github.io/2021/07/09/Python-DS-79-mpl3d/

 

Matplotlib 3D Plots (1)

Matplotlib으로 3D Plot을 할 수 있습니다. 많은 분들이 알고 있는 사실이지만 적극적으로 쓰이지 않습니다. 막상 쓰려면 너무 낯설기도 하고 잘 모르기도 하기 때문입니다. Reference matplotlib tutorial: The

jehyunlee.github.io

 

'Data > Visualization' 카테고리의 다른 글

[Matplotlib] plot  (0) 2023.12.17

0. 참고 공식 문서

https://cloud.google.com/bigquery/docs/reference/standard-sql/geography_functions

 

Geography functions  |  BigQuery  |  Google Cloud

GoogleSQL for BigQuery supports geography functions. Geography functions operate on or generate GoogleSQL GEOGRAPHY values. The signature of most geography functions starts with ST_. GoogleSQL for BigQuery supports the following functions that can be used

cloud.google.com

GoogleSQL for BigQuery supports geography functions. Geography functions operate on or generate GoogleSQL GEOGRAPHY values. The signature of most geography functions starts with ST_. GoogleSQL for BigQuery supports the following functions that can be used to analyze geographical data, determine spatial relationships between geographical features, and construct or manipulate GEOGRAPHYs.

 

 

1. ST_GEOPOINT 함수

ST_GEOPOINT 함수는 위도(latitude)와 경도(longitude) 값을 입력받아 지리적 좌표(Geography point)를 반환하는 함수.

이 함수는 위도와 경도를 사용하여 특정 지리적 지점을 정의할 때 유용합니다.

 

ST_GEOPOINT(longitude, latitude)

 

  • longitude: 경도 값. (-180 ~ 180 사이의 값), latitude: 위도 값. (-90 ~ 90 사이의 값)

이 함수는 경도(longitude)가 첫 번째 인자로, 위도(latitude)가 두 번째 인자로 들어간다는 점에 유의해야 합니다. 반환값은 Geography 타입입니다.

 

예시

SELECT ST_GEOPOINT(-122.4194, 37.7749) AS geopoint;

 

결과

POINT(-122.4194 37.7749)

 

 

2. ST_DISTANCE 함수

ST_DISTANCE 함수는 두 지리적 좌표(Geography objects) 사이의 거리를 계산해 줌.

이 함수는 미터 단위로 두 점 사이의 최단 거리를 반환하며, 구면 지구 모델을 사용하여 거리를 계산합니다. 이는 지리적 분석에서 두 위치 간의 물리적 거리를 구할 때 유용합니다.

ST_DISTANCE(geography1, geography2)

 

 

  • geography1: 첫 번째 지리적 좌표 (Geography 타입), geography2: 두 번째 지리적 좌표 (Geography 타입).

 

예시: 샌프란시스코와 뉴욕 사이의 거리를 계산

SELECT ST_DISTANCE(
  ST_GEOPOINT(-122.4194, 37.7749),  -- San Francisco
  ST_GEOPOINT(-74.0060, 40.7128)    -- New York
) AS distance;

 

결과: 

4130107.673 meters (약 4,130km)

 

3. ST_DISTANCE와 ST_GEOPOINT를 함께 사용하는 경우

WITH cities AS (
  SELECT 'San Francisco' AS city, ST_GEOPOINT(-122.4194, 37.7749) AS location UNION ALL
  SELECT 'New York' AS city, ST_GEOPOINT(-74.0060, 40.7128) AS location UNION ALL
  SELECT 'Los Angeles' AS city, ST_GEOPOINT(-118.2437, 34.0522) AS location
)
SELECT city, ST_DISTANCE(ST_GEOPOINT(-123.1207, 49.2827), location) AS distance
FROM cities
ORDER BY distance ASC
LIMIT 1;

위 쿼리는 캐나다 벤쿠버(경도 -123.1207, 위도 49.2827)와 가장 가까운 도시를 찾음.

 

결과:

Los Angeles (약 1734km)

 

 

4. 기타 같이 사용하면 좋을 것

1. Geolite mmdb

https://github.com/P3TERX/GeoLite.mmdb

 

GitHub - P3TERX/GeoLite.mmdb: MaxMind's GeoIP2 GeoLite2 Country, City, and ASN databases

MaxMind's GeoIP2 GeoLite2 Country, City, and ASN databases - P3TERX/GeoLite.mmdb

github.com

이전에 python 라이브러리들을 통해서 몇가지 처리를 하려하였지만 open api들에 대해선 request limit이 걸려 다음과 같은 mmdb를 사용.

 

 

2. GeoPandas

import geopandas as gpd
from shapely.geometry import Point

data = {'City': ['San Francisco', 'New York'],
        'Latitude': [37.7749, 40.7128],
        'Longitude': [-122.4194, -74.0060]}
gdf = gpd.GeoDataFrame(data, geometry=gpd.points_from_xy(data['Longitude'], data['Latitude']))


print(gdf)

결과:

 

 

3. Geopy

from geopy.distance import geodesic

san_francisco = (37.7749, -122.4194)
new_york = (40.7128, -74.0060)

distance = geodesic(san_francisco, new_york).meters
print(f"Distance between San Francisco and New York: {distance} meters")

결과:

 

 

4. Folium (시각화)

import folium

m = folium.Map(location=[37.7749, -122.4194], zoom_start=12)

folium.Marker([37.7749, -122.4194], popup="San Francisco").add_to(m)
folium.Marker([40.7128, -74.0060], popup="New York").add_to(m)

m.save("map.html")  # 지도 저장

 

(로컬에 저장된 html 파일을 크롬으로 드래그 앤 드롭 하면 열린다.)

 

'Data > SQL' 카테고리의 다른 글

[BigQuery] DECLARE, SET  (0) 2023.11.29
GCP BigQuery 공식 Documentation 링크
 
 
 
 

DECLARE


Description

지정된 유형의 변수를 선언.
DEFAULT 절이 지정된 경우 변수는 expression의 값으로 초기화되며, DEFAULT 절이 없으면 변수는 NULL 값으로 초기화됩니다.
 
 
 
Syntax
DECLARE variable_name[, ...] [variable_type] [DEFAULT expression];
 
 
Examples
DECLARE fromdate DATE DEFAULT '2014-01-01';  -- dates for after 2013
DECLARE todate DATE DEFAULT '2015-01-01';

SELECT FORMAT('From %t to %t', fromdate, todate);
 
이게 되는지 처음 알았다.
 
 

SET

 

Syntax

SET variable_name = expression;
SET (variable_name[, ...]) = (expression[, ...]);

 

 

 

Description

변수에 제공된 표현식의 값을 설정하거나 여러 변수를 여러 표현식의 결과를 기반으로 동시에 설정합니다.

SET 문은 다중 문장 쿼리 내에서 어디에서든 나타날 수 있습니다.

 

 

Examples

SET x = 5;
SET (a, b, c) = (1 + 3, 'foo', false);

'Data > SQL' 카테고리의 다른 글

[BigQuery] functions ST_DISTANCE, ST_GEOPOINT  (0) 2023.12.05

+ Recent posts