Questions tagged [matplotlib]

Matplotlib is a plotting library for Python which may be used interactively or embedded in stand-alone GUIs. Its compact "pyplot" interface is similar to the plotting functions of MATLAB®.

Matplotlib is a comprehensive plotting and visualization library for the Python programming language and its NumPy numerical mathematics extension. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like tkinter, wxPython, Qt, or GTK, Jupyter and IPython notebooks.

There is also a procedural "pyplot" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB.

Matplotlib can output PNG, PDF, PS, EPS, TIFF, JPEG, PGF, and SVG files, or plot in windows using a variety of toolkits (including plot animation).

Latest release information on Matplotlib's site.

Resources to get started:

71303 questions
536
votes
9 answers

Improve subplot size/spacing with many subplots

I need to generate a whole bunch of vertically-stacked plots in matplotlib. The result will be saved using savefig and viewed on a webpage, so I don't care how tall the final image is, as long as the subplots are spaced so they don't overlap. No…
mcstrother
  • 6,867
  • 5
  • 22
  • 18
535
votes
10 answers

Hiding axis text in matplotlib plots

I'm trying to plot a figure without tickmarks or numbers on either of the axes (I use axes in the traditional sense, not the matplotlib nomenclature!). An issue I have come across is where matplotlib adjusts the x(y)ticklabels by subtracting a value…
Dave
  • 6,184
  • 8
  • 26
  • 28
512
votes
1 answer

Installation Issue with matplotlib Python

I have issue after installing the matplotlib package unable to import matplotlib.pyplot as plt. Any suggestion will be greatly appreciate. >>> import matplotlib.pyplot as plt Traceback (most recent call last): File "", line 1, in…
J4cK
  • 30,459
  • 8
  • 42
  • 54
501
votes
9 answers

How to change legend fontsize with matplotlib.pyplot

Simple question here: I'm trying to get the size of my legend using matplotlib.pyplot to be smaller (i.e., the text to be smaller). The code I'm using goes something like this: plot.figure() plot.scatter(k, sum_cf, color='black', label='Sum of…
mike
  • 22,931
  • 31
  • 77
  • 100
490
votes
13 answers

How to change the figure size of a seaborn axes or figure level plot

How do I change the size of my image so it's suitable for printing? For example, I'd like to use to A4 paper, whose dimensions are 11.7 inches by 8.27 inches in landscape orientation.
Michael Grazebrook
  • 5,361
  • 3
  • 16
  • 18
485
votes
17 answers

How to remove axis, legends, and white padding

I would like to apply colormap to an image, and write the resulting image, without using axes, labels, titles, or anything automatically added by matplotlib. Here is what I did: def make_image(inputname,outputname): data =…
sunmat
  • 6,976
  • 3
  • 28
  • 44
484
votes
10 answers

Make tick labels font size smaller

In a matplotlib figure, how can I make the font size for the tick labels using ax1.set_xticklabels() smaller? Further, how can one rotate it from horizontal to vertical?
Open the way
  • 26,225
  • 51
  • 142
  • 196
482
votes
12 answers

How to remove xticks from a plot

I have a semilogx plot and I would like to remove the xticks. I tried: plt.gca().set_xticks([]) plt.xticks([]) ax.set_xticks([]) The grid disappears (ok), but small ticks (at the place of the main ticks) remain. How to remove them?
Vincent
  • 57,703
  • 61
  • 205
  • 388
478
votes
6 answers

Adding a matplotlib legend

How can one create a legend for a line graph in Matplotlib's PyPlot without creating any extra variables? Please consider the graphing script below: if __name__ == '__main__': PyPlot.plot(total_lengths, sort_times_bubble, 'b-', …
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
477
votes
8 answers

How to adjust padding with cutoff or overlapping labels

Updated MRE with subplots I'm not sure of the usefulness of the original question and MRE. The margin padding seems to be properly adjusted for large x and y labels. The issue is reproducible with subplots. Using matplotlib 3.4.2 fig, axes =…
Andrew
  • 5,375
  • 3
  • 17
  • 12
475
votes
31 answers

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm

I am trying to plot a simple graph using pyplot, e.g.: import matplotlib.pyplot as plt plt.plot([1,2,3],[5,7,4]) plt.show() but the figure does not appear and I get the following message: UserWarning: Matplotlib is currently using agg, which is a…
johnwolf1987
  • 10,603
  • 3
  • 14
  • 17
471
votes
6 answers

How to draw vertical lines on a given plot

Given a plot of a signal in time representation, how can I draw lines marking the corresponding time index? Specifically, given a signal plot with a time index ranging from 0 to 2.6 (seconds), I want to draw vertical red lines indicating the…
Francis
  • 6,416
  • 5
  • 24
  • 32
460
votes
11 answers

Secondary axis with twinx(): how to add to legend

I have a plot with two y-axes, using twinx(). I also give labels to the lines, and want to show them with legend(), but I only succeed to get the labels of one axis in the legend: import numpy as np import matplotlib.pyplot as plt from matplotlib…
joris
  • 133,120
  • 36
  • 247
  • 202
460
votes
6 answers

Why do many examples use `fig, ax = plt.subplots()`

I'm learning to use matplotlib by studying examples, and a lot of examples seem to include a line like the following before creating a single plot... fig, ax = plt.subplots() Here are some examples... Modify tick label…
neelshiv
  • 6,125
  • 6
  • 21
  • 35
446
votes
10 answers

How to add a title to each subplot

I have one figure which contains many subplots. fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k') fig.canvas.set_window_title('Window Title') # Returns the Axes instance ax = fig.add_subplot(311) ax2 =…
Shailen
  • 7,909
  • 3
  • 29
  • 37