Matplotlib - Blog
User plt.scatter to plot a scatter plot.
fig = plt.figure(figsize=(14, 7), dpi=80) plt.scatter('x_column_name', 'y_column_name', data=df, s='size_column_name', c='color_column_name')
In Matlab like syntax how do you get and clear current axes and figure?
get current axes: plt.gca() get current figure: plt.gcf() clear current axes: plt.cla() clear current figure: plt.clf()
Format >> green dots with a scatter plot?
go >> line left blank for scatter.
Format >> green dots with solid line?
go-
Use plt.GridSpec to create custom grid.
import matplotlib.gridspec as gridspec fig = plt.figure() grid = plt.GridSpec(#rows, #cols) plt.subplot(grid[row_start:row_end, col_start:col_end]) ... repeat for each graph
Import matplotlib and use the inline feature.
import matplotlib.pyplot as plt %matplotlib inline
How to plot an inner plot (a plot over a plot).
inner_ax = fig.add_axes([x_point, y_point, width, height])
Format >> black squares with dotted line?
ks.
How to add annotation to a plot?
plt.annotate( list of parameters)
Explicitly set the legend of a plot. The plot has four graphs, show the border, and set the border transparency to 1, set the number of columns to 2, show shadow, set the border thickness to 1, and set the title.
plt.legend( [graph1, graph2, graph3, graph4], # plot items ['Graph 1', 'Graph 2', 'Graph 3', 'Graph 4'], # names frameon=True, # border framealpha=1, # border transparency ncol=2, # number of columns shadow=True, # shadow borderpad=1, # border thickness title='Some title goes here') # title
How to get a list of the available styles and to set the plotting style to one of them?
plt.style.available plt.style.set('style name')
How can you add text in relation to data, axes, and figure?
plt.text(x_value, y_value, 'some text', transform=some_option) data >> some_option = plt.gca().transData axes >> some_option = plt.gca().transAxes figure >> some_option = plt.gca().transFigure
How to set the X axis ticks? using Matlab and OO syntax.
plt.xticks() ax.set_xticks() ax.set_xticklabels()
Format >> red stars with dashed lines?
r*--
How to draw two sets of scatter plots in same plot?
Simply call plt.plot() again before plt.show()
Matplotlib returns the plot object itself besides drawing the plot. How do you only show the plot itself?
add the following line of code at the end: plt.show()
Pseudo code for bar chart.
ax.bar(x_data, y_data, alignment='center', width=0.5, alpha=0.5)
Pseudo code for box plot.
ax.boxplot(some_series)
Pseudo code for fill between.
ax.fill_between(x_data, y1_data, y2_data, color='name of color', alpha=0.5)
Use plt.subplot2grid to create custom grid.
fig = plt.figure() ax_i = plt.subplot2grid((#rows, #cols), (current row, current col), colspan=x, rowspan=x)
What are the 3 basic arguments that plt.plot() expects?
(x, y, format) The format is a short hand combination of {color}{marker}{line}.
What is the difference between Matlab like syntax and Object Oriented like syntax in matplotlib?
The matlab syntax is 'stateful'. That means, the plt keeps track of what the current axes is. So whatever you draw with plt.{anything} will reflect only on the current subplot. Practically speaking, the main difference between the two syntaxes is, in matlab-like syntax, all plotting is done using plt methods instead of the respective axes's method as in object oriented syntax.
Pseudo code for histogram.
ax.hist(some_series)
Pseudo code for scatter plot.
ax.scatter(x_data, y_data)
Pseudo code for step chart.
ax.step(x_data, y_data)
How to set multiple features on an axes at once?
ax1.set(title='Scatterplot Greendots', xlabel='X', ylabel='Y', xlim=(0,6), ylim=(0,12))
How to plot with 2 axes?
ax2 = ax1.twinx() ax2.plot(x_data, y2_data, color='give it a different color')
Format >> blue diamonds with dash-dot line?
bD-.
Pseudo code for time series.
dates = pd.date_range('yyyy-mm-dd start point date', periods=some_number) ax.plot(dates, y_data_series) ax.set_xticks(dates[::30]) ax.set_xticklabels(dates.strftime('%Y-%m-%d)[::30])