티스토리 뷰

python

matplotlib 연습

swanB 2017. 1. 10. 17:13

import matplotlib as mpl

import matplotlib.pylab as plt

import numpy as np



xlist = [10,20,30,40]

ylist = [1,4,9,16]


plt.plot(xlist, ylist ,'rs:')

plt.plot(ylist, c="b", lw=5, ls="--", marker="o", ms="15", mec="g", mew=5,)

plt.hold(True)

plt.plot([9,16, 4, 1], c="k", lw=3, ls=":", marker="s", ms=10, mec="m", mew=5, mfc="c")

plt.hold(False)

plt.xlim(-0.2, 3.2)

plt.ylim(-1,18)

plt.show()







-------------------------------------------------------------

x = np.linspace(-np.pi, np.pi, 256)

c, s = np.cos(x), np.sin(x)

plt.plot(x,c, label='cosine')

plt.hold(True)

plt.plot(x,s, label='sin')

plt.hold(False)

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], 

           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])

plt.yticks([-1,0,1], ["Low", "Zero", "High"])

plt.grid(True)

plt.legend(loc=0)


plt.xlabel("time")

plt.ylabel("aplitude")

plt.title("Cosine Plot")

plt.show()



---------------------------------------------------------------



t = np.arange(0., 5., 0.2)

plt.plot(t,t, 'r--', t, 0.5*t**2, 'bs:', t, 0.2*t**3, 'g^-')

plt.show()


-------------------------------------------------------------

plt.plot(x, s, label='sine')

plt.hold(True)

plt.scatter([0], [0], color='r', linewidth=10)

plt.annotate(r'$(0,0)$', xy=(0,0), xycoords='data', xytext=(-50,50),

            textcoords='offset points', fontsize=16,

            arrowprops=dict(arrowstyle="->", linewidth=3, color="g"))

plt.show()


-------------------------------------------------------------


f1 = plt.figure(figsize=(10,2))

plt.hold(True)

plt.plot(np.random.randn(100), 'r-', np.random.randn(100), 'b-')

plt.show()


-------------------------------------------------------------

f1 = plt.figure(1)

plt.plot([1,2,3,4], 'ro:')

f2 = plt.gcf()  #figure 를 호출하지 않아도 객체를 받을 수 있다

print(f1, id(f1))

print(f2, id(f2))

plt.show()





-------------------------------------------------------------

x1 = np.linspace(0.0, 5.0)

x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)

y2 = np.cos(2 * np.pi * x2)


ax1 = plt.subplot(2,1,1)

plt.plot(x1,y1,'yo-')

plt.title('A tale of 2 subplots')

plt.ylabel('Damped oscillation')

print(ax1)



ax2 = plt.subplot(2,1,2)

plt.plot(x2,y2,'r.-')

plt.xlabel('time (s)')

plt.ylabel('Undamped')

print(ax2)

plt.show()



-------------------------------------------------------------

plt.subplot(221); plt.plot([1,2]); plt.title(1)

plt.subplot(222); plt.plot([1,2]); plt.title(2)

plt.subplot(223); plt.plot([1,2]); plt.title(3)

plt.subplot(224); plt.plot([1,2]); plt.title(4)

plt.tight_layout()

plt.show()




---------------------------------------------------------------


with plt.xkcd():

    plt.title('XKCD style plot!!!')

    plt.plot(x,c, label="cosine")

    t = 2 * np.pi / 3

    plt.scatter(t, np.cos(t), 50, color='blue')

    plt.annotate(r'0.5 Here!', xy=(t,np.cos(t)), xycoords='data',xytext=(-90,-50),

                 textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))

    

    

plt.show()  


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함