Skip to content

Instantly share code, notes, and snippets.

@iros iros/0.plt.txt
Last active Aug 29, 2015

What would you like to do?
matplotlib style attributes
alpha Alpha (transparency) of the plot – default is 1 (no transparency)
color Color description for the line.1
label Label for the line – used when creating legends
linestyle A line style symbol
linewidth A positive integer indicating the width of the line
marker A marker shape symbol or character
markeredgecolor Color of the edge (a line) around the marker
markeredgewidth Width of the edge (a line) around the marker
markerfacecolor Face color of the marker
markersize A positive integer indicating the size of the marker
# get help
setp(h, ’linestyle’)
linestyle: [ ‘‘’-’‘‘ | ‘‘’--’‘‘ | ‘‘’-.’‘‘ | ‘‘’:’‘‘ | ‘‘’None’‘‘ | ‘‘’ ’‘‘ | ‘‘’’‘‘ ]
and any drawstyle in combination with a linestyle, e.g. ‘‘’steps--’‘‘.
setp(h, ’linestyle’, '''---''')
from matplotlib.pyplot import figure, plot, bar, pie, draw, scatter
from numpy.random import randn, rand
from numpy import sqrt, arange
fig = figure()
# Add the subplot to the figure
# Panel 1
ax = fig.add_subplot(2, 2, 1)
y = randn(100)
# Panel 2
y = rand(5)
x = arange(5)
ax = fig.add_subplot(2, 2, 2)
bar(x, y)
# Panel 3
y = rand(5)
y = y / sum(y)
y[y < .05] = .05
ax = fig.add_subplot(2, 2, 3)
# Panel 4
z = randn(100, 2)
z[:, 1] = 0.5 * z[:, 0] + sqrt(0.5) * z[:, 1]
x = z[:, 0]
y = z[:, 1]
ax = fig.add_subplot(2, 2, 4)
scatter(x, y)


figure is used to open a figure window, and can be used to generate axes. fig = figure(n) produces a figure object with id n, and assigns the object to fig.


add_subplot is used to add axes to a figure. ax = fig.add_subplot(111) can be used to add a basic axes to a figure. ax = fig.add_subplot(m,n,i) can be used to add an axes to a non-trivial figure with a m by n grid of plots.


close closes figures. close(n) closes the figure with id n, and close(’all’) closes all figure windows.


show is used to force an update to a figure, and pauses execution if not used in an interactive console (close the figure window to resume execution). show should not be used in standalone Python programs – draw should be used instead.


draw forces an update to a figure.


Save to file: savefig(’filename.ext’) ext can be png, gif, jpg, svg etc.

T = 100
x = []
for i in xrange(1,T+1):
y = cumsum(rnd.randn(T))
fig = figure()
ax = fig.add_subplot(111)
# A call to autofmt_xdate() can be used to address the issue of overlapping labels
# further customize time labels:
# %Y - 4 digit numeric year
# %m - Numeric month
# %d - Numeric day
# %b - Short month name
# %H - Hour
# %M - Minute
# %D - Named day
months = mdates.MonthLocator()
fmt = mdates.DateFormatter(’%b %Y’)
# make sure the first tick on the left is showing
xlim = list(ax.get_xlim())
xlim[0] = mdates.date2num(dt.datetime(2012,3,1))

subselecting columns

Single: d['Destination'] or d.Destination

multiple: d[['Destination', 'Year', 'Refugees', 'AsylumSeekers']]

select rows by boolean condition

st = d['col'] != '*'

Selecting subset of columns and rows:

d.ix[rows, cols]

d.ix[st, ['col1', 'col2']

delete column:

del d[’col1’]

data frame to a stack

x = d.stack()

results in:

Year                       1968
Destination             Lebanon
Origin         Syrian Arab Rep.
Refugees                   1200
Total                      1200
dtype: object

now I can call


Handy for iteration?

d.unstack() back to dataframe.


merge and join provide SQL-like operations for merging the DataFrames using row labels or the contents of columns. The primary difference between the two is that merge defaults to using column contents while join defaults to using index labels. Both commands take a large number of optional inputs. The important keyword arguments are:

  • how, which must be one of ’left’, ’right’, ’outer’, ’inner’ describes which set of indices to use when performing the join. ’left’ uses the indices of the DataFrame that is used to call the method and ’right’ uses the DataFrame input into merge or join. ’outer’ uses a union of all indices from both DataFrames and ’inner’ uses an intersection from the two DataFrames.
  • on is a single column name of list of column names to use in the merge. on assumes the names are common. If no value is given for on or left_on/right_on, then the common column names are used.
  • left_on and right_on allow for a merge using columns with different names. When left_on and right_on contains the same column names, the behavior is the same as on.
  • left_index and right_index indicate that the index labels are the join key for the left and right DataFrames.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.