다운로드
작성자: admin 작성일시: 2016-04-08 16:48:45 조회수: 3686 다운로드: 295
카테고리: Python 태그목록: Python Matplotlib seaborn bokeh Visualization

Python 시각화 시작하기

Python 시각화 라이브러리

  • matplotlib
  • seaborn
  • bokeh

Prerequisite

inline graphics (Jupyter Notebook)

%matplotlib inline

Backend format

In:
%config InlineBackend.figure_formats
Out:
{'svg'}
In:
%config InlineBackend.figure_formats = ['svg']

inline figure size

In:
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)

Basic plot

plot(y)

  • x = range(len(y))
In:
plt.plot([1,2,3,4])
plt.show()

plot(x, y)

In:
plt.plot([1,2,3,4], [1,4,9,16])
plt.show()

plot(xy, color)

In:
plt.plot([1,2,3,4], [1,4,9,16], 'r')
plt.show()
In:
mpl.colors.cnames
Out:
{u'aliceblue': u'#F0F8FF',
 u'antiquewhite': u'#FAEBD7',
 u'aqua': u'#00FFFF',
 u'aquamarine': u'#7FFFD4',
 u'azure': u'#F0FFFF',
 u'beige': u'#F5F5DC',
 u'bisque': u'#FFE4C4',
 u'black': u'#000000',
 u'blanchedalmond': u'#FFEBCD',
 u'blue': u'#0000FF',
 u'blueviolet': u'#8A2BE2',
 u'brown': u'#A52A2A',
 u'burlywood': u'#DEB887',
 u'cadetblue': u'#5F9EA0',
 u'chartreuse': u'#7FFF00',
 u'chocolate': u'#D2691E',
 u'coral': u'#FF7F50',
 u'cornflowerblue': u'#6495ED',
 u'cornsilk': u'#FFF8DC',
 u'crimson': u'#DC143C',
 u'cyan': u'#00FFFF',
 u'darkblue': u'#00008B',
 u'darkcyan': u'#008B8B',
 u'darkgoldenrod': u'#B8860B',
 u'darkgray': u'#A9A9A9',
 u'darkgreen': u'#006400',
 u'darkgrey': u'#A9A9A9',
 u'darkkhaki': u'#BDB76B',
 u'darkmagenta': u'#8B008B',
 u'darkolivegreen': u'#556B2F',
 u'darkorange': u'#FF8C00',
 u'darkorchid': u'#9932CC',
 u'darkred': u'#8B0000',
 u'darksage': u'#598556',
 u'darksalmon': u'#E9967A',
 u'darkseagreen': u'#8FBC8F',
 u'darkslateblue': u'#483D8B',
 u'darkslategray': u'#2F4F4F',
 u'darkslategrey': u'#2F4F4F',
 u'darkturquoise': u'#00CED1',
 u'darkviolet': u'#9400D3',
 u'deeppink': u'#FF1493',
 u'deepskyblue': u'#00BFFF',
 u'dimgray': u'#696969',
 u'dimgrey': u'#696969',
 u'dodgerblue': u'#1E90FF',
 u'firebrick': u'#B22222',
 u'floralwhite': u'#FFFAF0',
 u'forestgreen': u'#228B22',
 u'fuchsia': u'#FF00FF',
 u'gainsboro': u'#DCDCDC',
 u'ghostwhite': u'#F8F8FF',
 u'gold': u'#FFD700',
 u'goldenrod': u'#DAA520',
 u'gray': u'#808080',
 u'green': u'#008000',
 u'greenyellow': u'#ADFF2F',
 u'grey': u'#808080',
 u'honeydew': u'#F0FFF0',
 u'hotpink': u'#FF69B4',
 u'indianred': u'#CD5C5C',
 u'indigo': u'#4B0082',
 u'ivory': u'#FFFFF0',
 u'khaki': u'#F0E68C',
 u'lavender': u'#E6E6FA',
 u'lavenderblush': u'#FFF0F5',
 u'lawngreen': u'#7CFC00',
 u'lemonchiffon': u'#FFFACD',
 u'lightblue': u'#ADD8E6',
 u'lightcoral': u'#F08080',
 u'lightcyan': u'#E0FFFF',
 u'lightgoldenrodyellow': u'#FAFAD2',
 u'lightgray': u'#D3D3D3',
 u'lightgreen': u'#90EE90',
 u'lightgrey': u'#D3D3D3',
 u'lightpink': u'#FFB6C1',
 u'lightsage': u'#BCECAC',
 u'lightsalmon': u'#FFA07A',
 u'lightseagreen': u'#20B2AA',
 u'lightskyblue': u'#87CEFA',
 u'lightslategray': u'#778899',
 u'lightslategrey': u'#778899',
 u'lightsteelblue': u'#B0C4DE',
 u'lightyellow': u'#FFFFE0',
 u'lime': u'#00FF00',
 u'limegreen': u'#32CD32',
 u'linen': u'#FAF0E6',
 u'magenta': u'#FF00FF',
 u'maroon': u'#800000',
 u'mediumaquamarine': u'#66CDAA',
 u'mediumblue': u'#0000CD',
 u'mediumorchid': u'#BA55D3',
 u'mediumpurple': u'#9370DB',
 u'mediumseagreen': u'#3CB371',
 u'mediumslateblue': u'#7B68EE',
 u'mediumspringgreen': u'#00FA9A',
 u'mediumturquoise': u'#48D1CC',
 u'mediumvioletred': u'#C71585',
 u'midnightblue': u'#191970',
 u'mintcream': u'#F5FFFA',
 u'mistyrose': u'#FFE4E1',
 u'moccasin': u'#FFE4B5',
 u'navajowhite': u'#FFDEAD',
 u'navy': u'#000080',
 u'oldlace': u'#FDF5E6',
 u'olive': u'#808000',
 u'olivedrab': u'#6B8E23',
 u'orange': u'#FFA500',
 u'orangered': u'#FF4500',
 u'orchid': u'#DA70D6',
 u'palegoldenrod': u'#EEE8AA',
 u'palegreen': u'#98FB98',
 u'paleturquoise': u'#AFEEEE',
 u'palevioletred': u'#DB7093',
 u'papayawhip': u'#FFEFD5',
 u'peachpuff': u'#FFDAB9',
 u'peru': u'#CD853F',
 u'pink': u'#FFC0CB',
 u'plum': u'#DDA0DD',
 u'powderblue': u'#B0E0E6',
 u'purple': u'#800080',
 u'red': u'#FF0000',
 u'rosybrown': u'#BC8F8F',
 u'royalblue': u'#4169E1',
 u'saddlebrown': u'#8B4513',
 u'sage': u'#87AE73',
 u'salmon': u'#FA8072',
 u'sandybrown': u'#FAA460',
 u'seagreen': u'#2E8B57',
 u'seashell': u'#FFF5EE',
 u'sienna': u'#A0522D',
 u'silver': u'#C0C0C0',
 u'skyblue': u'#87CEEB',
 u'slateblue': u'#6A5ACD',
 u'slategray': u'#708090',
 u'slategrey': u'#708090',
 u'snow': u'#FFFAFA',
 u'springgreen': u'#00FF7F',
 u'steelblue': u'#4682B4',
 u'tan': u'#D2B48C',
 u'teal': u'#008080',
 u'thistle': u'#D8BFD8',
 u'tomato': u'#FF6347',
 u'turquoise': u'#40E0D0',
 u'violet': u'#EE82EE',
 u'wheat': u'#F5DEB3',
 u'white': u'#FFFFFF',
 u'whitesmoke': u'#F5F5F5',
 u'yellow': u'#FFFF00',
 u'yellowgreen': u'#9ACD32'}

plot(xy, color-linestyle)

  • names linestyle
    • '-' or 'solid': solid line
    • '--' or 'dashed': dashed line
    • '-.' or 'dash_dot': dash-dotted line
    • ':' or 'dotted': dotted line
    • 'None': draw nothing
    • ' ': draw nothing
    • '': draw nothing
In:
plt.plot([1,2,3,4], [1,4,9,16], ':')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], 'r--')
plt.show()

plot(xy, color-linestyle-marker)

  • '.' point marker
  • ',' pixel marker
  • 'o' circle marker
  • 'v' triangle_down marker
  • '^' triangle_up marker
  • '<' triangle_left marker
  • '>' triangle_right marker
  • '1' tri_down marker
  • '2' tri_up marker
  • '3' tri_left marker
  • '4' tri_right marker
  • 's' square marker
  • 'p' pentagon marker
  • '*' star marker
  • 'h' hexagon1 marker
  • 'H' hexagon2 marker
  • '+' plus marker
  • 'x' x marker
  • 'D' diamond marker
In:
plt.plot([1,2,3,4], [1,4,9,16], 'o')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], color="magenta", linestyle="--", markeredgecolor='r', markerfacecolor="orange", marker="^", markersize=20)
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], 'r:o')
plt.show()

axis

  • axis([xmin, xmax, ymin, ymax])
  • axis('off'): turns off the axis lines and labels.:
  • axis('equal'): changes limits of x or y axis so that equal increments of x and y have the same length; a circle is circular.:
  • axis('scaled'): achieves the same result by changing the dimensions of the plot box instead of the axis data limits.:
  • axis('tight'): changes x and y axis limits such that all data is shown.
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis([0, 5, 0, 18])
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('off')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('equal')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('scaled')
plt.show()
In:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('tight')
plt.show()

Multiple Lines

  • plot(x1, y1, style1, x2, y2, style2, ...)
In:
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

Advanced Plot

In:
plt.figure(figsize=(8, 6))
X = np.linspace(-np.pi, np.pi, 256)
C, S = np.cos(X), np.sin(X)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")
plt.xlim(-4.0, 4.0)
plt.ylim(-1.0, 1.0)
plt.xticks(np.linspace(-4, 4, 9))
plt.yticks(np.linspace(-1, 1, 5))
plt.show()

figure

  • num: number of figure
  • figsize: figure size in in inches (width, height)
In:
# %matplotlib qt
f1 = plt.figure(1, figsize=(2, 2))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
f2 = plt.figure(2, figsize=(6, 6))
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.show()

plot argument names

In:
plt.figure()
plt.plot(X, C, color="blue", linewidth=2, linestyle="-")
plt.plot(X, S, color="red",  linewidth=5, linestyle="--")
Out:
[]

Limit

xmin, xmax = plt.xlim() 
xlim( (xmin, xmax) )  # set the xlim to xmin, xmax
xlim( xmin, xmax )    # set the xlim to xmin, xmax
In:
plt.figure()
plt.plot(X, C)
plt.xlim(X.min() * 1.1, X.max() * 1.1);
plt.ylim(C.min() * 1.1, C.max() * 1.1);
In:
plt.figure()
plt.plot(X, C)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi]);
plt.yticks([-1, 0, +1]);
In:
plt.figure()
plt.plot(X, C)
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], [r'$-1$', r'$0$', r'$+1$']);

Legend

In:
plt.plot(X, C, label="cosine")
plt.plot(X, S, label="sine")
plt.legend(loc='upper left')
Out:

Annotate

In:
plt.plot(X, C, label="cosine")
t = 2 * np.pi / 3
plt.scatter(t, np.cos(t), 50, color='blue')
plt.annotate(r'$cos(\frac{2\pi}{3})=-\frac{1}{2}$', xy=(t, np.cos(t)), xycoords='data', xytext=(-90, -50), 
             textcoords='offset points', fontsize=16, arrowprops=dict(arrowstyle="->"))
Out:

Grid

In:
plt.plot(X, C)
plt.grid(False)

xkcd style

In:
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="->"))