Matplotlib
keyboard_arrow_down 83 guides
chevron_leftMatplotlib
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Matplotlib | Recipes reference
schedule Aug 12, 2023
Last updated local_offer
Tags Python●Matplotlib
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
Axes Cookbook
- Drawing plots in logarithmic scale in MatplotlibTo draw plots in logarithmic scale in Matplotlib, use the following semilogy(~) method.
- Drawing the axis line in MatplotlibTo draw the axis line in Matplotlib, use the axhline(~) and axvline(~) methods.
- Inverting y-axis in MatplotlibTo invert the y-axis in Matplotlib use the invert_yaxis() method.
- Plotting two lines with different y axis in MatplotlibWe can plot two lines with different y axis in Matplotlib using the twinx() method.
- Removing axis in MatplotlibTo completely remove an axis in Matplotlib, set plt.axis("off").
- Setting only the lower or upper limit in MatplotlibTo set only the lower or upper limit for plt.xlim(~), set either the left or right parameter. For plt.ylim(~), set either the bottom or top parameter.
- Setting x-axis limit in MatplotlibIt is possible to set a limit on the x-axis (i.e. the domain) in Matplotlib using the xlim(~) method.
- Setting y-axis limit in MatplotlibIt is possible to set a limit on the y-axis (i.e. the range) in Matplotlib using the y-lim(~) method.
Colors Cookbook
- Adding multiple plots with different colors in MatplotlibBy default, Matplotlib actually changes the color of the additional plots you add.
- Changing background color of figure in MatplotlibIt is possible to change the background color of a figure in Matplotlib using set_facecolor(~).
- Changing color based on value in MatplotlibTo change the color of a data point based on its value in Matplotlib, we can leverage the cmap parameter within the plt.scatter(~) method.
- Changing line color of a plot in MatplotlibIt is possible to change the line color of a plot in Matplotlib by passing the color argument to plt.plot(~).
- Changing the background color of a plot in MatplotlibTo change the background color of a plot in Matplotlib use the set_facecolor(~) method.
- Changing the color of axes in MatplotlibIt is possible to change the color of axes in Matplotlib using the set_color(~) method.
- Changing the color of tick labels in MatplotlibTo change the color of tick labels in Matplotlib use the tick_params(~) method.
- Drawing semi-transparent plots in MatplotlibTo draw semi-transparent plots in Matplotlib, set the alpha parameter in the plot(~) method.
- Filling in the area above a curve in MatplotlibTo fill in the area above a curve in Matplotlib, use the plt.fill_between(~) method.
- Filling in the area underneath a curve in MatplotlibTo fill the area underneath a curve in Matplotlib, use the plt.fill_between(~) method.
- Getting specific color value from a color map in MatplotlibTo get a specific color value from a color map in Matplotlib use the cm.get_cmap(~) method.
- Inverting color maps in MatplotlibTo invert a color map in Matplotlib, attach the _r suffix to the color map.
- Making the background figure transparent in MatplotlibWe can make the background figure transparent in Matplotlib using the set_alpha(~) method.
- Shading an area in MatplotlibTo shade an area in Matplotlib, use the fill(~) method.
Customizing Plots Cookbook
- Changing line width in MatplotlibWe can change the line width in Matplotlib by specifying the keyword-argument linewidth.
- Changing size of figures in MatplotlibWe can change the size of figures in Matplotlib using the figsize argument or using the set_size_inches method.
- Changing the plot style in MatplotlibTo change the plot style in Matplotlib use the the plt.style.use(~) method.
- Removing grid lines in MatplotlibTo remove grid lines in Matplotlib, use the plt.grid(~) method and pass False argument.
- Setting the linestyle in MatplotlibWe can specify the linestyle of a plot using the linestyle parameter of the plot(~) method.
- Showing grid lines in MatplotlibIt is possible to show grid lines in Matplotlib using the plot.grid(~) method.
Graphs Cookbook
- Drawing a bar chart in MatplotlibTo draw bar charts in Matplotlib we can use the ax.bar(~) method.
- Drawing a box plot in MatplotlibTo draw a box plot in Matplotlib use the boxplot(~) method.
- Drawing a function in MatplotlibHere we provide a helper function to help to draw functions in Matplotlib.
- Drawing a histogram in MatplotlibWe can draw a histogram in Matplotlib using the plt.hist(~) method.
- Drawing a horizontal line in MatplotlibTo draw a horizontal line in Matplotlib, use the axhline(~) function.
- Drawing a line plot in MatplotlibTo draw a basic line plot in Matplotlib, use the plt.plot(~) function.
- Drawing a normal curve in MatplotlibTo draw the normal curve, we need to use the norm.pdf(~) method of the scipy.stats library.
- Drawing a scatterplot in MatplotlibTo draw a basic 2D scatter plot in Matplotlib, we can use the scatter(~) function.
- Drawing a single point in MatplotlibTo draw a single point in Matplotlib, use the plt.scatter(~) function.
- Drawing a stacked bar chart in MatplotlibTo draw stacked bar charts in Matplotlib we can use the ax.bar(~) method.
- Drawing a vertical line in MatplotlibTo draw a vertical line in Matplotlib, use the axvline(~) function.
- Drawing arrows in MatplotlibIn Matplotlib, use the arrow(~) method to draw arrows.
- Drawing circles in MatplotlibTo draw circles in Matplotlib, create a Circle object and add it into the axis using the add_artist() method.
- Drawing empty circles in MatplotlibTo draw empty circles in Matplotlib we can specify facecolors="none" within the plt.scatter(~) method.
- Drawing error bars in MatplotlibTo draw error bars in Matplotlib, use the plt.errorbar(~) method.
- Drawing horizontal bar plots in MatplotlibIn Matplotlib, we can draw a horizontal bar plot using the barh(~) method
- Drawing multiple histograms in one plot in MatplotlibTo draw multiple histograms in one plot in Matplotlib using the plt.hist(~) method.
- Normalizing a histogram in MatplotlibWe can normalize a histogram in Matplotlib using the density keyword argument and setting it to True. By normalizing a histogram, the sum of the bar area equals 1.
- Plotting scatter plot with category in MatplotlibTo plot a scatter plot with categories or classes in Matplotlib, supply the c and cmap arguments to plt.scatter(~).
Legend Cookbook
- Adding a legend to a plot in MatplotlibTo add a legend to a plot in Matplotlib pass through the label parameter.
- Adding a single legend to subplots in MatplotlibWe can use the get_legend_handles_labels() method on the last axis only to add a single legend to subplots in Matplotlib.
- Placing the legend outside the plot in MatplotlibWe can place the legend outside the plot in Matplotlib using the bbox_to_anchor argument of the legend method.
- Removing the legend in MatplotlibWe can remove the legend in Matplotlib using the remove() method.
- Specifying exact location of the legend in MatplotlibTo specify the exact location of the legend in Matplotlib, set the bbox_to_anchor and loc parameters of the legend(~) method.
Miscellaneous Cookbook
- Displaying an image in MatplotlibWe can import an image from a file in Matplotlib using the imread method.
- Opening a plot in a window in Jupyter Notebook for MatplotlibIn Jupyter Notebook, adding the following line of code "%matplotlib qt" will open plots in a new window.
- Resetting rcparams to default settings in MatplotlibTo reset rcparams to default settings in Matplotlib we can use plt.rcParams.update(mpl.rcParamsDefault).
- Showing a plot inline in Jupyter Notebook for MatplotlibInstead of showing the plot in a new window, we can display the plot directly in Jupyter Notebook by executing the following line of code: %matplotlib inline.
Saving Cookbook
- Saving a graph in MatplotlibIt is possible to save a graph as png, svg or pdf format in Matplotlib.
- Saving a graph as a svg in MatplotlibTo save your graph as a svg in Matplotlib pass the format="svg" argument to the savefig method.
Subplots Cookbook
- Adding a main title to subplots in MatplotlibWe can add a main title to subplots in Matplotlib using the suptitle(~) method.
- Adding title to subplots in MatplotlibTo add a title to the subplots in Matplotlib, use the title.set_text(~) method.
- Creating subplots in MatplotlibWe can create subplots in Matplotlib by calling plt.subplots(~).
- Fixing spacing between subplots in MatplotlibWe can fix the spacing between subplots in Matplotlib using the subplots_adjust(~) method.
- Making plots side by side in MatplotlibTo draw plots side by side in Matplotlib, use the subplots(~) method.
- Making subplots share the same axis in MatplotlibWhen creating subplots in Matplotlib, we can make the subplots share the same x axis or y axis by passing sharex=True or sharey=True to the plt.subplots(~) call.
Text, Label and Annotations Cookbook
- Adding a title to a plot in MatplotlibIt is possible to add a title to a plot in Matplotlib using plt.title(~).
- Adding arrows to an annotation in MatplotlibWe can add arrows to an annotation in Matplotlib by passing the arrowprops argument to the ax.annotate(~) method.
- Adding axis labels to a plot in MatplotlibIt is possible to add axis labels to a plot in Matplotlib using plt.xlabel(~) and plt.ylabel(~).
- Adding markers to a plot in MatplotlibWe can add markers to a plot in Matplotlib by passing the marker argument when calling the plot(~) method.
- Annotating data points in MatplotlibTo annotate data points in Matplotlib, use the annotate(~) method.
- Applying an offset to annotations in MatplotlibTo apply an offset to annotations in Matplotlib we leverage the xytext and textcoords parameters within the plt.annotate(~) method.
- Changing the default font size in MatplotlibWe can change the default font size in Matplotlib using plt.rcParams.update(~) method.
- Changing the marker size of scatter plots in MatplotlibWe can specify the s argument in plt.scatter(~) to change the size of all markers in Matplotlib.
- Changing the number of ticks in MatplotlibWe can change the number of ticks in Matplotlib by specifying the nbins argument of locator_params method.
- Changing the tick size in MatplotlibTo change the tick size in Matplotlib, use the tick_params(~) method.
- Removing certain ticks in MatplotlibWe can remove certain ticks in Matplotlib by using the set_visible(~) method.
- Removing column name label from pie charts in MatplotlibTo remove the column name label from pie charts in Matplotlib we can set the y-axis label to not be visible.
- Removing default axis labels in MatplotlibTo remove default axis labels in Matplotlib, specify xticks and yticks with empty values.
- Rotating axis labels in MatplotlibTo rotate axis labels in Matplotlib, use the xticks(~) and the yticks(~) method.
- Rotating custom tick labels in MatplotlibTo rotate custom tick labels in Matplotlib we can leverage the set_xticklabels(~) method.
- Specifying custom tick labels in MatplotlibWe can specify custom tick labels in Matplotlib using the xticks() method.
- Writing mathematical expressions in MatplotlibTo write mathematical expressions in Matplotlib, precede the expression with an r and then place the mathematical expression within a pair of dollar signs ($).
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!