๐Ÿ“ˆ3D Graph1

Gyeomiiยท2022๋…„ 7์›” 14์ผ
0

DDITPython

๋ชฉ๋ก ๋ณด๊ธฐ
16/18
post-thumbnail

ํŒŒ์ด์ฌ์—์„œ 3D ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ

์ฝ”๋“œ

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.plot([0, 0, 0], [0, 2, 4], [0, 5, 0], '')
#ax.plot([x1, x2, x3], [y1, y2, y3], [z1, z2, z3], '')

plt.show()
  • ์ A (x1, y1, z1)์„ ์ขŒํ‘œ ์œ„์— ์ฐ์„ ๋•Œ ax.plot([x1], [y1], [z1])์ด ๋œ๋‹ค.
  • ๊ฐ™์€ ์›๋ฆฌ๋กœ ์  2๊ฐœ๋ฅผ ์ฐ์œผ๋ฉด ์ง์„ ์ด, 3๊ฐœ๋ฅผ ์ฐ์œผ๋ฉด ๊บพ์€์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ์ 3๊ฐœ๋ฅผ ์ฐ์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
ax.plot([x1, x2, x3], [y1, y2, y3], [z1, z2, z3], '์ƒ‰์ƒ, ์„ ์˜ํ˜•ํƒœ๋“ฑ ์†์„ฑ ์ถ”๊ฐ€')

๊ฒฐ๊ณผ

์ฝ”๋“œ

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.plot([0, 0, 0], [0, 2, 4], [0, 5, 0], '')
ax.plot([1, 1, 1], [0, 2, 4], [0, 5, 0], '')
ax.plot([-1, -1, -1], [0, 2, 4], [0, 5, 0], '')
#ax.plot([x1, x2, x3], [y1, y2, y3], [z1, z2, z3], '')

plt.show()
  • plot์„ 3๊ฐœ๋ฅผ ๋‘๋ฉด ๊ทธ๋ž˜ํ”„ 3๊ฐœ๋ฅผ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ๊ฐ x์ขŒํ‘œ๋งŒ ๋‹ค๋ฅธ ๊ฐ™์€ ํ˜•ํƒœ์˜ ๊ทธ๋ž˜ํ”„๋‹ค.

๊ฒฐ๊ณผ

ํฌ๋กค๋งํ•˜์—ฌ DB์— ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ๋กœ ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ

์ฝ”๋“œ

import matplotlib.pyplot as plt
from day14.stockdao import StockDao

sd = StockDao()
#DAO์‚ฌ์šฉํ•ด์„œ DB์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
samsung = sd.select("์‚ผ์„ฑ์ „์ž")
sk = sd.select("SK")
lg = sd.select("LG")
# ๊ฐ€๊ฒฉ์„ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
samPriceList = []
lgPriceList = []
skPriceList = []
# ๊ฐ x์ขŒํ‘œ๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
x1 = []
x2 = []
x3 = []
# ๊ฐ y์ขŒํ‘œ๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
y1 = []
y2 = []
y3 = []
# samsung ๋ฐ์ดํ„ฐ ๊บผ๋‚ด์˜ค๊ธฐ
for idx, i in enumerate(samsung):
    firstPrice = samsung[0].get('price')
    price = i.get('price')
    gap = price - firstPrice
    percent = gap/firstPrice*100
    samPriceList.append(percent)
    x1.append(0)
    y1.append(idx)
    

for idx, i in enumerate(lg):
    firstPrice = lg[0].get('price')
    price = i.get('price')
    gap = price - firstPrice
    percent = gap/firstPrice*100
    lgPriceList.append(percent)
    x2.append(1)
    y2.append(idx)
    
for idx, i in enumerate(sk):
    firstPrice = sk[0].get('price')
    price = i.get('price')
    gap = price - firstPrice
    percent = gap/firstPrice*100
    skPriceList.append(percent)
    x3.append(2)
    y3.append(idx)

fig = plt.figure()

ax = fig.add_subplot(1,1,1, projection='3d')       

ax.plot(x1, y1, samPriceList,'r')
ax.plot(x2, y2, lgPriceList,'g')
ax.plot(x3, y3, skPriceList,'b')

plt.show()
  • ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ธˆ์•ก๋งŒ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๋Š”๋‹ค.
for idx, i in enumerate(samsung):
	# ๊ทธ๋ž˜ํ”„๋ฅผ %๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ๋น„๊ตํ•  ์ฒซ ๊ฐ€๊ฒฉ
    firstPrice = samsung[0].get('price')
	# samsung์— {'price':10000}์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ๋œ dictํƒ€์ž… ๋ฐ์ดํ„ฐ์—์„œ ๊ธˆ์•ก๋งŒ ๊บผ๋‚ด์˜ค๊ธฐ
    price = i.get('price')
	# ์ฒซ ๊ธˆ์•ก๊ณผ ๊บผ๋‚ด์˜จ ๊ธˆ์•ก๊ณผ์˜ ์ฐจ์•ก์„ ๊ตฌํ•จ
    gap = price - firstPrice
	# ๊ทธ ์ฐจ์ด๋ฅผ %๋กœ ๋‚˜ํƒ€๋ƒ„
    percent = gap/firstPrice*100
	# ํผ์„ผํ‹ฐ์ง€๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๊ธฐ (z์ถ•์„ ๊ตฌ์„ฑ)
    samPriceList.append(percent)
	# x์ขŒํ‘œ๋ฅผ ์ „๋ถ€ 0์œผ๋กœ ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜๋งŒํผ x1๋ฆฌ์ŠคํŠธ์— 0์„ ์ถ”๊ฐ€
    x1.append(0)
	# y์ขŒํ‘œ๋ฅผ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋‹น 1์นธ ์”ฉ ์ด๋™์‹œํ‚ค๊ธฐ ์œ„ํ•ด idx๊ฐ’์„ y1์— ์ถ”๊ฐ€
    y1.append(idx)

fig = plt.figure()

ax = fig.add_subplot(1,1,1, projection='3d')       
plt.show()

๊ฒฐ๊ณผ

  • ๋ณ€๋™ํ•œ ๊ฐ€๊ฒฉ์˜ ๋น„์œจ๋กœ ๋‚˜ํƒ€๋ƒˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ˆˆ์— ๋šœ๋ ทํ•˜๊ฒŒ ๋ณ€ํ™”๊ฐ€ ๋ณด์ธ๋‹ค.
  • ๊ฐ€๊ฒฉ์˜ ๋น„์œจ์ด ์•„๋‹ˆ๋ผ ๊ฐ€๊ฒฉ ๊ทธ ์ž์ฒด๋ฅผ ๋น„๊ตํ•œ๋‹ค๋ฉด ํ•œ ๊ทธ๋ž˜ํ”„ ์•ˆ์—์„œ ๋ณ€ํ™”ํ•˜๋Š” ํญ์ด ๋ฏธ๋ฏธํ•˜๊ฒŒ ๋ณด์ธ๋‹ค

  • ์ขŒํ‘œ์˜ ์ƒํ•œ, ํ•˜ํ•œ ๊ฐ’์„ ์ •ํ•ด์ค„ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
    ax.set_xlim3d(-1, 3)
    ax.set_ylim3d(0, 30)
    ax.set_zlim3d(50000, 200000)
profile
๊น€์„ฑ๊ฒธ

0๊ฐœ์˜ ๋Œ“๊ธ€