[Matplotlib](https://matplotlib.org/ is a powerful two-dimensional plotting library for the Python language. Matplotlib is capable of creating all manner of graphs, plots, charts, histograms, and much more.
In most cases, matplotlib will simply output the chart to your viewport when the .show()
method is invoked, but we’ll briefly explore how to save a matplotlib creation to an actual file on disk.
While the feature-list of matplotlib is nearly limitless, we’ll quickly go over how to use the library to generate a basic chart for your own testing purposes.
Box Plot with plotly.express¶ Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. In a box plot created by px.box, the distribution of the column given as y argument is represented. The Python matplotlib scatter plot is a two dimensional graphical representation of the data. A Python scatter plot is useful to display the correlation between two numerical data values or two data sets. In general, we use this matplotlib scatter plot to analyze the relationship between two numerical data points by drawing a regression line. Sep 14, 2020.plot is a wrapper for pyplot.plot , and the result is a graph identical to the one you produced with Matplotlib: You can use both pyplot.plot and df.plot to produce the same graph from columns of a DataFrame object. However, if you already have a DataFrame instance, then df.plot offers cleaner syntax than pyplot.plot.
Like all Python libraries, you’ll need to begin by installing matplotlib. We won’t go through the installation process here, but there’s plenty of information in the official documentation.
Once installed, import the matplotlib
library. You’ll likely also want to import the pyplot
sub-library, which is what you’ll generally be using to generate your charts and plots when using matplotlib.
Now to create and display a simple chart, we’ll first use the .plot()
method and pass in a few arrays of numbers for our values. For this example, we’ll plot the number of books read over the span of a few months.
We can also add a few axis labels:
Finally, we can display the chart by calling .show()
:
With a simple chart under our belts, now we can opt to output the chart to a file instead of displaying it (or both if desired), by using the .savefig()
method.
The .savefig()
method requires a filename be specified as the first argument. This filename can be a full path and as seen above, can also include a particular file extension if desired. If no extension is provided, the configuration value of savefig.format
is used instead.
In addition to the basic functionality of saving the chart to a file, .savefig()
also has a number of useful optional arguments.
dpi
can be used to set the resolution of the file to a numeric value.transparent
can be set to True
, which causes the background of the chart to be transparent.bbox_inches
can be set to alter the size of the bounding box (whitespace) around the output image. In most cases, if no bounding box is desired, using bbox_inches='tight'
is ideal.bbox_inches
is set to 'tight'
, then the pad_inches
option specifies the amount of padding around the image.There are a handful of additional options for specific occasions, but overall this should get you started with easily generating image file outputs from your matplotlib charts.