Back to Cheatsheets

lmatplotlib

Table of Contents

1. ABOUT

2. FORMATTING

3. LINE GRAPHS

4. BAR GRAPHS

5. PIE CHART

6. HISTOGRAM

7. SCATTER

8. BOXPLOT

ABOUT

Import

from matplotlib import pyplot as plt

Show

Close

FORMATTING

Labels

plt.xlabel('x_name_string')
plt.ylabel('y_name_string'')
plt.title('title_string')

Legend

Set axis

plt.axis(arg)

Modify ticks

ax = plt.subplot(args)
plt.plot(args)
ax.set_xticks([values_for_ticks])
ax.set_yticks([values_for_ticks], rotation=#)

Subplot

plt.subplot(#rows, #cols. index_plot_creating)
plt.plot()

Figure

Figures and Subplots

fig = plt.figure()
fig.add_subplot()

Seaborn color

sns.set()
for col in 'xy':
  plt.hist(data[col], normed=True, alpha=0.5)

LINE GRAPH

plt.plot(x_values, y_values, color='color_name', linestyle='style', marker='marker', label='for_legend', linewidth=#width)

Multiple

plt.plot(x1, y1)
plt.plot(x2, y2)

Line Format

Shade Error

y_lower = [i - error_value for i in y_values]
y_upper = [i + error_value for i in y_values]
plt.fill_between(x_values, y_lower, y_upper, aplha=transp_amount)

BAR CHART

plt.bar(x_values, y_values)

Multiple

x_values1 = [t*element + w*n for element
             in range(d)]

Stacked

plt.bar(x1, y1)
plt.bar(x2, y2, bottom=y1)

Error Bars

plt.bar(x_values, y_values, yerr=error_value, capsize=cap_width)

PIE CHART

plt.pit(data_list, 
        labels=category_list, 
        autopct='format', 
        colors=list_colors
        shadow=True
        startangle=#
        pctdistance=#
        explode=explode)

HISTOGRAM

plt.hist(dataset, range=(min, max), bins=#bins, edgecolor = 'black')

Multiple

plt.hist(a, range=(min, max), bins=#bins, alpha=#transp, normed=true)
plt.hist(b, range=(min, max), bins=#bins, alpha=#transp, normed=true)

SCATTER

plt.scatter(x_values, y_values)

3D

fig_3d = plt.figure()
fig_3d.add_subplot(1,1,1,projection='3d')
plt.scatter(x,y,z)

BOXPLOTS

plt.boxplot(dataset)
or
plt.boxplot([dataset1, dataset2])