Skip to content

Instantly share code, notes, and snippets.

@durgaswaroop
Last active June 13, 2018 10:33
Show Gist options
  • Save durgaswaroop/225cebe4a666760bf1650200c2eaca0b to your computer and use it in GitHub Desktop.
Save durgaswaroop/225cebe4a666760bf1650200c2eaca0b to your computer and use it in GitHub Desktop.
Matplotlib related things

In Jupyter notebooks %matplotlib notebook sets up the backend to render the visualizations into the browser.

There are several backends for matplotlib including even those that render to hardcopies like svg or png.

Matplotlib's naming conventions are matlabish than pythonic. We use getters and setters.

Layers:

  1. Artist Layer

Artist Layer:

Abstraction to making drawing stuff easy. There is a figure object with one or more subplots. Each subplot is a series with one or more axes. Also contains base items for drawing like rectangle, line, ellipse and collection of items like paths (a combination of lines)

Plotting

plt.xlabel('X-axis name') # SEt x-axis label
plt.ylabel('Y-axis name') # SEt y-axis label

plt.plot(ds, color='blue', label='Original')  # Plot Indexed dataset ds with a blue line. Label is displayed in the legend. Index column becomes the X-axis

plt.legend(loc='best')  # Display legend in the 'best' location

plt.title('Plot Title')  # Set Plot's title

If you want subplots:

plt.subplot(2,1,1) # 2 Rows, 1 Column, 1st Plot
plt.plot(ds_logscale, color='blue')

plt.subplot(2,1,2) # 2 Rows, 1 Column, 2nd Plot
plt.plot(ds_logscale_diff_shift)

plt.show()

To draw lines on the graph at arbitrary postions either horizontal or vertical:

plt.axhline(y=1, linestyle='--', color='gray')  # Draws a dashed gray horizontal line at y=1, parallel to X-axis. 
plt.axvline(x=1, linestyle='--', color='gray')  # Draws a dashed gray vertical line at x=1, parallel to Y-axis. 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment