Python Data Visualization

Updated at

Pandas

  • df.plot(x=‘col’, y=‘col’, kind=‘scatter’)
    • c=‘color’, color=‘color’
    • figsize=(,), ax=ax1, grid=True
    • label=’’, legend=True, title=’’
    • xlim=(,), xticks=[]
    • rot=30, alpha=1
    • autopct=’%.1f%%’ # -% String Formatting -.1 precision -f fixed point -% perc -% symbol
    • secondary_y=False, marker=‘o’
  • df.plot.bar(x=‘col’, y=‘col’)
  • df.plot.kde()
  • df.hist(bins=, range=(,), histtype=‘step’)
  • df.box(x=‘col’, y=‘col’)
  • df.<graph>()
  • from pandas.plotting import scatter_matrix
    • scatter_matrix(cols, figsize(,))

Seaborn

Seaborn BasicsSyntax
Import moduleimport seaborn as sns
Set background stylesns.set_style('darkgrid' | 'whitegrid' | 'dark' | 'white' | 'ticks')
Remove spinessns.despine(left=True, bottom=True)
Histogram \w KDEsns.distplot(y_values)
Kernel Density Plotsns.kdeplot(y_values, shade=True)
Countplot (Clustered Bar)sns.countplot(x='x_col', hue='y_col', data=df, order=[], hue_order=[])
Strip Plot (Narrow Scatter)sns.stripplot(x='x_col', y='y_col', data=df, jitter=True)
Box Plot (And Whisker)sns.boxplot(x='x_col', y='y_col', data=df, whis=4, orient='vertical', width=.15)
Heatmapsns.heatmap(data, cmap='Blues', cbar=False, annot=False, yticklabels=False)
Small Multipleg = sns.FacetGrid(df, col='col1', row='col2', hue='col3', size=height)
Fill Small Multipleg.map(sns.kdeplot, 'y_col', shade=True)

Matplotlib

Matplotlib BasicsSyntax
Import moduleimport matplotlib.pyplot as plt
Jupyter inline%matplotlib inline
Set plot styleplt.style.use('fivethirtyeight')
Plot style listplt.style.available
Show plotplt.show()
Save plotplt.savefig('file') | fig.savefig('file')
Create figurefig = plt.figure(figsize=(width_dpi, height_dpi))
Add plot to the figureax = fig.add_subplot(nrows, ncols, plot_number)
Create figure and subplotsfig, axes = plt.subplots(nrows, ncols, figsize=(width, height))
Disable spinesax.spines['side'].set_visible(False) | right, bottom, top, left
Matplotlib ChartsSyntax
Line chartplt.plot(x_values, y_values, c='color', label='', linewidth=3)
Bar plotplt.bar(bar_positions, bar_heights, [bar_width])
Horizontal bar plotplt.barh(bar_positions, bar_widths, [bar_height])
Scatter plotplt.scatter(x, y)
Histogramplt.hist(y_values, bins=int, range=(min, max))
Box plotplt.boxplot(values)
Matplotlib Plot and AxispltaxArguments
Set Titletitleset_title'text'
Add Legendlegendlegend'text', loc='upper left', fontsize=12
Set Axis Labelsxlabelset_xlabel'text', size=12
Ticks and Their Labelsxticksset_xticks, ax.set_xticklabels[ticks], [labels], rotation=90, size=12
Batch Tick Parameterstick_paramstick_paramsbottom='off', top='off', left='off', right='off', labelbottom='off', labelsize=12
Set Axis Limit Rangexlimset_xlimmin, max
Add H/V Linesaxhlineaxhliney, label='', c='color', alpha=1
Add Texttexttextx, y, 'text'