Python 시각화 시작하기

Python 시각화 라이브러리

  • matplotlib
  • seaborn
  • bokeh

Prerequisite

inline graphics (Jupyter Notebook)

%matplotlib inline

Backend format


In [1]:
%config InlineBackend.figure_formats


Out[1]:
{'png'}

In [3]:
%config InlineBackend.figure_formats = ['svg']

inline figure size


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

Basic plot

plot(y)

  • x = range(len(y))

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


plot(x, y)


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


plot(xy, color)


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



In [17]:
mpl.colors.cnames


Out[17]:
{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 [19]:
plt.plot([1,2,3,4], [1,4,9,16], ':')
plt.show()



In [22]:
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 [23]:
plt.plot([1,2,3,4], [1,4,9,16], 'o')
plt.show()



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



In [25]:
plt.plot([1,2,3,4], [1,4,9,16], color="magenta", linestyle="--", markeredgecolor='r', markerfacecolor="orange", marker="^", markersize=20)
plt.show()



In [26]:
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 [27]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis([0, 5, 0, 18])
plt.show()



In [28]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('off')
plt.show()



In [29]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('equal')
plt.show()



In [30]:
plt.plot([1,2,3,4], [1,4,9,16], '-o')
plt.axis('scaled')
plt.show()



In [32]:
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 [35]:
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 [37]:
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 [38]:
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 [39]:
plt.figure()
plt.plot(X, C, color="blue", linewidth=2, linestyle="-")
plt.plot(X, S, color="red",  linewidth=5, linestyle="--")


Out[39]:
[<matplotlib.lines.Line2D at 0xd41ceb8>]

Limit

xmin, xmax = plt.xlim() 
xlim( (xmin, xmax) )  # set the xlim to xmin, xmax
xlim( xmin, xmax )    # set the xlim to xmin, xmax

In [40]:
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 [43]:
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 [46]:
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 [48]:
plt.plot(X, C, label="cosine")
plt.plot(X, S, label="sine")
plt.legend(loc='upper left')


Out[48]:
<matplotlib.legend.Legend at 0xd12d4a8>

Annotate


In [49]:
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[49]:
<matplotlib.text.Annotation at 0xd257320>

Grid


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


xkcd style


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


Advanced Settings

  • Axes
  • Axis
  • Tick

Axes

Axis

  • http://matplotlib.org/api/axis_api.html

  • cla()

  • get_label()
  • get_label_text()
  • get_major_ticks(numticks=None)
  • get_majorticklabels()
  • get_majorticklines()
  • get_majorticklocs()
  • get_minor_ticks(numticks=None)
  • get_minorticklabels()
  • get_minorticklines()
  • get_minorticklocs()
  • get_offset_text()
  • get_ticklabels(minor=False, which=None)
  • get_ticklines(minor=False)
  • get_ticklocs(minor=False)
  • grid(b=None, which='major', **kwargs)
  • set_label_text(label, fontdict=None, **kwargs)
  • set_ticklabels(ticklabels, *args, **kwargs)
  • set_ticks(ticks, minor=False)

Tick

Other plots

Scatter Plot


In [55]:
X = np.random.normal(0,1,1024)
Y = np.random.normal(0,1,1024)
plt.scatter(X,Y)


Out[55]:
<matplotlib.collections.PathCollection at 0xdff9d30>

Pie chart


In [56]:
with plt.xkcd():
    plt.figure(figsize=(5, 5))
    Z = np.random.uniform(0, 1, 20)
    plt.pie(Z, colors=mpl.cm.Set2(np.arange(20)/20.));
    plt.axis('equal');


Bar chart


In [57]:
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1):
    plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
for x, y in zip(X, -Y2):
    plt.text(x + 0.4, y - 0.05, '%.2f' % y, ha='center', va='top')
plt.xlim(-0.25, 12)
plt.ylim(-1.25, +1.25)


Out[57]:
(-1.25, 1.25)

Histogram


In [58]:
x = np.random.randn(5000)
arrays, bins, patches = plt.hist(x, bins=50, normed=True)



In [59]:
arrays


Out[59]:
array([ 0.00291973,  0.00145986,  0.00437959,  0.00875918,  0.01167891,
        0.01751836,  0.02335782,  0.02627755,  0.02481768,  0.05401496,
        0.07737277,  0.09927073,  0.09927073,  0.13722718,  0.18394282,
        0.21168023,  0.21022037,  0.27445437,  0.29051287,  0.34890741,
        0.34744755,  0.39416319,  0.4014625 ,  0.37372509,  0.4058421 ,
        0.36496591,  0.38978359,  0.34890741,  0.33138905,  0.289053  ,
        0.28467341,  0.25401628,  0.19124214,  0.192702  ,  0.13284759,
        0.12116868,  0.07883264,  0.07299318,  0.06131427,  0.0481755 ,
        0.03649659,  0.02773741,  0.01313877,  0.01459864,  0.00437959,
        0.00291973,  0.00437959,  0.00145986,  0.        ,  0.00145986])

In [60]:
bins


Out[60]:
array([-3.26401985, -3.12702076, -2.99002166, -2.85302257, -2.71602347,
       -2.57902438, -2.44202528, -2.30502619, -2.16802709, -2.03102799,
       -1.8940289 , -1.7570298 , -1.62003071, -1.48303161, -1.34603252,
       -1.20903342, -1.07203432, -0.93503523, -0.79803613, -0.66103704,
       -0.52403794, -0.38703885, -0.25003975, -0.11304066,  0.02395844,
        0.16095754,  0.29795663,  0.43495573,  0.57195482,  0.70895392,
        0.84595301,  0.98295211,  1.11995121,  1.2569503 ,  1.3939494 ,
        1.53094849,  1.66794759,  1.80494668,  1.94194578,  2.07894487,
        2.21594397,  2.35294307,  2.48994216,  2.62694126,  2.76394035,
        2.90093945,  3.03793854,  3.17493764,  3.31193674,  3.44893583,
        3.58593493])

In [61]:
patches


Out[61]:
<a list of 50 Patch objects>

Contour plot


In [62]:
def f(x, y):
    return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(-x ** 2 -y ** 2)

n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
XX, YY = np.meshgrid(x, y)
ZZ = f(XX, YY)
plt.contourf(XX, YY, ZZ, alpha=.75, cmap='jet')
plt.contour(XX, YY, ZZ, colors='black', linewidth=.5)


Out[62]:
<matplotlib.contour.QuadContourSet at 0xf740a90>

Imshow


In [63]:
plt.imshow(ZZ, interpolation='nearest')
plt.grid(False)



In [64]:
plt.imshow(ZZ)
plt.grid(False)


3D surface


In [65]:
# %matplotlib qt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
XX, YY = np.meshgrid(X, Y)
RR = np.sqrt(XX**2 + YY**2)
ZZ = np.sin(RR)
ax.plot_surface(XX, YY, ZZ, rstride=1, cstride=1, cmap='hot')


Out[65]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0xd7aba20>

ColorMap

  • 숫자 또는 순서를 color로 변환하는 함수 또는 표

In [66]:
mpl.cm.cmap_d


Out[66]:
{u'Accent': <matplotlib.colors.LinearSegmentedColormap at 0x8037f98>,
 u'Accent_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a320>,
 u'Blues': <matplotlib.colors.LinearSegmentedColormap at 0x809a358>,
 u'Blues_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a780>,
 u'BrBG': <matplotlib.colors.LinearSegmentedColormap at 0x809a128>,
 u'BrBG_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a240>,
 u'BuGn': <matplotlib.colors.LinearSegmentedColormap at 0x80376a0>,
 u'BuGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a198>,
 u'BuPu': <matplotlib.colors.LinearSegmentedColormap at 0x8037b38>,
 u'BuPu_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037fd0>,
 u'CMRmap': <matplotlib.colors.LinearSegmentedColormap at 0x80372b0>,
 u'CMRmap_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037eb8>,
 u'Dark2': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1be0>,
 u'Dark2_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1e10>,
 u'GnBu': <matplotlib.colors.LinearSegmentedColormap at 0x80370f0>,
 u'GnBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a208>,
 u'Greens': <matplotlib.colors.LinearSegmentedColormap at 0x80377f0>,
 u'Greens_r': <matplotlib.colors.LinearSegmentedColormap at 0x80370b8>,
 u'Greys': <matplotlib.colors.LinearSegmentedColormap at 0x809a390>,
 u'Greys_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a4a8>,
 u'OrRd': <matplotlib.colors.LinearSegmentedColormap at 0x8037c50>,
 u'OrRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037278>,
 u'Oranges': <matplotlib.colors.LinearSegmentedColormap at 0x8037e10>,
 u'Oranges_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037940>,
 u'PRGn': <matplotlib.colors.LinearSegmentedColormap at 0x8037828>,
 u'PRGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a470>,
 u'Paired': <matplotlib.colors.LinearSegmentedColormap at 0x80378d0>,
 u'Paired_r': <matplotlib.colors.LinearSegmentedColormap at 0x80376d8>,
 u'Pastel1': <matplotlib.colors.LinearSegmentedColormap at 0x8037a20>,
 u'Pastel1_r': <matplotlib.colors.LinearSegmentedColormap at 0x80379e8>,
 u'Pastel2': <matplotlib.colors.LinearSegmentedColormap at 0x80379b0>,
 u'Pastel2_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037390>,
 u'PiYG': <matplotlib.colors.LinearSegmentedColormap at 0x8037e80>,
 u'PiYG_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a710>,
 u'PuBu': <matplotlib.colors.LinearSegmentedColormap at 0x80374e0>,
 u'PuBuGn': <matplotlib.colors.LinearSegmentedColormap at 0x809a978>,
 u'PuBuGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1d30>,
 u'PuBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037c88>,
 u'PuOr': <matplotlib.colors.LinearSegmentedColormap at 0x809a940>,
 u'PuOr_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1c88>,
 u'PuRd': <matplotlib.colors.LinearSegmentedColormap at 0x809a2e8>,
 u'PuRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037a90>,
 u'Purples': <matplotlib.colors.LinearSegmentedColormap at 0x809a828>,
 u'Purples_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037630>,
 u'RdBu': <matplotlib.colors.LinearSegmentedColormap at 0x809a5c0>,
 u'RdBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a0f0>,
 u'RdGy': <matplotlib.colors.LinearSegmentedColormap at 0x809a2b0>,
 u'RdGy_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037518>,
 u'RdPu': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1d68>,
 u'RdPu_r': <matplotlib.colors.LinearSegmentedColormap at 0x80375f8>,
 u'RdYlBu': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1e80>,
 u'RdYlBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x80371d0>,
 u'RdYlGn': <matplotlib.colors.LinearSegmentedColormap at 0x8037358>,
 u'RdYlGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037e48>,
 u'Reds': <matplotlib.colors.LinearSegmentedColormap at 0x809a160>,
 u'Reds_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a278>,
 u'Set1': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1b00>,
 u'Set1_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a908>,
 u'Set2': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1b38>,
 u'Set2_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037470>,
 u'Set3': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1b70>,
 u'Set3_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037c18>,
 u'Spectral': <matplotlib.colors.LinearSegmentedColormap at 0x7fe10f0>,
 u'Spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037ac8>,
 u'Wistia': <matplotlib.colors.LinearSegmentedColormap at 0x8037240>,
 u'Wistia_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1a90>,
 u'YlGn': <matplotlib.colors.LinearSegmentedColormap at 0x8037ef0>,
 u'YlGnBu': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1e48>,
 u'YlGnBu_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037b70>,
 u'YlGn_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a748>,
 u'YlOrBr': <matplotlib.colors.LinearSegmentedColormap at 0x809a630>,
 u'YlOrBr_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a7b8>,
 u'YlOrRd': <matplotlib.colors.LinearSegmentedColormap at 0x80377b8>,
 u'YlOrRd_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037438>,
 u'afmhot': <matplotlib.colors.LinearSegmentedColormap at 0x809a9e8>,
 u'afmhot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1cc0>,
 u'autumn': <matplotlib.colors.LinearSegmentedColormap at 0x809a3c8>,
 u'autumn_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a898>,
 u'binary': <matplotlib.colors.LinearSegmentedColormap at 0x809a518>,
 u'binary_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037f28>,
 u'bone': <matplotlib.colors.LinearSegmentedColormap at 0x80372e8>,
 u'bone_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037da0>,
 u'brg': <matplotlib.colors.LinearSegmentedColormap at 0x8037cf8>,
 u'brg_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1ba8>,
 u'bwr': <matplotlib.colors.LinearSegmentedColormap at 0x8037748>,
 u'bwr_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a860>,
 u'cool': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1fd0>,
 u'cool_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1f98>,
 u'coolwarm': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1a58>,
 u'coolwarm_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a668>,
 u'copper': <matplotlib.colors.LinearSegmentedColormap at 0x8037ba8>,
 u'copper_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037080>,
 u'cubehelix': <matplotlib.colors.LinearSegmentedColormap at 0x8037780>,
 u'cubehelix_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a400>,
 u'flag': <matplotlib.colors.LinearSegmentedColormap at 0x809a080>,
 u'flag_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037588>,
 u'gist_earth': <matplotlib.colors.LinearSegmentedColormap at 0x8037dd8>,
 u'gist_earth_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037be0>,
 u'gist_gray': <matplotlib.colors.LinearSegmentedColormap at 0x809a048>,
 u'gist_gray_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037f60>,
 u'gist_heat': <matplotlib.colors.LinearSegmentedColormap at 0x8037860>,
 u'gist_heat_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037208>,
 u'gist_ncar': <matplotlib.colors.LinearSegmentedColormap at 0x8037128>,
 u'gist_ncar_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1da0>,
 u'gist_rainbow': <matplotlib.colors.LinearSegmentedColormap at 0x8037198>,
 u'gist_rainbow_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1ef0>,
 u'gist_stern': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1f28>,
 u'gist_stern_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037320>,
 u'gist_yarg': <matplotlib.colors.LinearSegmentedColormap at 0x8037668>,
 u'gist_yarg_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1dd8>,
 u'gnuplot': <matplotlib.colors.LinearSegmentedColormap at 0x809a588>,
 u'gnuplot2': <matplotlib.colors.LinearSegmentedColormap at 0x8037d30>,
 u'gnuplot2_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037b00>,
 u'gnuplot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1f60>,
 u'gray': <matplotlib.colors.LinearSegmentedColormap at 0x8037048>,
 u'gray_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037a58>,
 u'hot': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1c50>,
 u'hot_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1eb8>,
 u'hsv': <matplotlib.colors.LinearSegmentedColormap at 0x8037908>,
 u'hsv_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037710>,
 'inferno': <matplotlib.colors.ListedColormap at 0x7fe1208>,
 'inferno_r': <matplotlib.colors.ListedColormap at 0x7fe1240>,
 u'jet': <matplotlib.colors.LinearSegmentedColormap at 0x8037d68>,
 u'jet_r': <matplotlib.colors.LinearSegmentedColormap at 0x80375c0>,
 'magma': <matplotlib.colors.ListedColormap at 0x7fe1160>,
 'magma_r': <matplotlib.colors.ListedColormap at 0x7fe11d0>,
 u'nipy_spectral': <matplotlib.colors.LinearSegmentedColormap at 0x809a9b0>,
 u'nipy_spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a438>,
 u'ocean': <matplotlib.colors.LinearSegmentedColormap at 0x809a8d0>,
 u'ocean_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037cc0>,
 u'pink': <matplotlib.colors.LinearSegmentedColormap at 0x809a4e0>,
 u'pink_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1ac8>,
 'plasma': <matplotlib.colors.ListedColormap at 0x7fe1278>,
 'plasma_r': <matplotlib.colors.ListedColormap at 0x7fe12b0>,
 u'prism': <matplotlib.colors.LinearSegmentedColormap at 0x809a5f8>,
 u'prism_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037978>,
 u'rainbow': <matplotlib.colors.LinearSegmentedColormap at 0x809a6d8>,
 u'rainbow_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a6a0>,
 u'seismic': <matplotlib.colors.LinearSegmentedColormap at 0x809a7f0>,
 u'seismic_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a0b8>,
 u'spectral': <matplotlib.colors.LinearSegmentedColormap at 0x8037550>,
 u'spectral_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037898>,
 u'spring': <matplotlib.colors.LinearSegmentedColormap at 0x80373c8>,
 u'spring_r': <matplotlib.colors.LinearSegmentedColormap at 0x8037160>,
 u'summer': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1a20>,
 u'summer_r': <matplotlib.colors.LinearSegmentedColormap at 0x809a1d0>,
 u'terrain': <matplotlib.colors.LinearSegmentedColormap at 0x8037400>,
 u'terrain_r': <matplotlib.colors.LinearSegmentedColormap at 0x7fe1cf8>,
 'viridis': <matplotlib.colors.ListedColormap at 0x7fe12e8>,
 'viridis_r': <matplotlib.colors.ListedColormap at 0x7fe1320>,
 u'winter': <matplotlib.colors.LinearSegmentedColormap at 0x809a550>,
 u'winter_r': <matplotlib.colors.LinearSegmentedColormap at 0x80374a8>}

In [67]:
cm1 = plt.get_cmap('gray')
print(cm1(0))
print(cm1(0.5))
print(cm1(1))


(0.0, 0.0, 0.0, 1.0)
(0.50196078431372548, 0.50196078431372548, 0.50196078431372548, 1.0)
(0.0039215686274509803, 0.0039215686274509803, 0.0039215686274509803, 1.0)

In [68]:
cm2 = mpl.colors.ListedColormap(['red', 'green', 'blue'])
print(cm2(np.linspace(0, 1, 10)))


[[ 1.          0.          0.          1.        ]
 [ 1.          0.          0.          1.        ]
 [ 1.          0.          0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.50196078  0.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]
 [ 0.          0.          1.          1.        ]]

In [69]:
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
plt.subplot(311);
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('Set2'))
plt.grid(False)
plt.subplot(312);
plt.imshow(gradient, aspect='auto', cmap=cm1)
plt.grid(False)
plt.subplot(313);
plt.imshow(gradient, aspect='auto', cmap=cm2)
plt.grid(False)


Seaborn


In [70]:
d = np.random.normal(size=100)
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
sns.distplot(d, color="m", ax=axes[1, 1])


Out[70]:
<matplotlib.axes._subplots.AxesSubplot at 0xf462048>

In [71]:
df = sns.load_dataset("iris")
sns.pairplot(df, hue="species")


Out[71]:
<seaborn.axisgrid.PairGrid at 0xf405470>

In [72]:
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)


Bokeh


In [73]:
from bokeh.plotting import figure, output_notebook, show
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
output_notebook()
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p.line(x, y, legend="Temp.", line_width=2)
show(p)


Loading BokehJS ...
Out[73]:

<Bokeh Notebook handle for In[73]>