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®.
72,115
questions
3207
votes
16
answers
6.1m
views
How do I change the size of figures drawn with Matplotlib?
How do I change the size of figure drawn with Matplotlib?
1735
votes
26
answers
2.9m
views
Save plot to image file instead of displaying it
This displays the figure in a GUI:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
But how do I instead save the figure to a file (e.g. foo.png)?
1638
votes
18
answers
1.9m
views
How to put the legend outside the plot
I have a series of 20 plots (not subplots) to be made in a single figure. I want the legend to be outside of the box. At the same time, I do not want to change the axes, as the size of the figure gets ...
948
votes
11
answers
1.4m
views
How to make IPython notebook matplotlib plot inline
I am trying to use IPython notebook on MacOS X with Python 2.7.2 and IPython 1.1.0.
I cannot get matplotlib graphics to show up inline.
import matplotlib
import numpy as np
import matplotlib.pyplot ...
941
votes
12
answers
1.0m
views
Purpose of "%matplotlib inline"
What exactly is the use of %matplotlib inline?
913
votes
16
answers
2.1m
views
How to change the font size on a matplotlib plot
How does one change the font size for all elements (ticks, labels, title) on a matplotlib plot?
I know how to change the tick label sizes, this is done with:
import matplotlib
matplotlib.rc('xtick',...
905
votes
12
answers
2.5m
views
How do I set the figure title and axes labels font size?
I am creating a figure in Matplotlib like this:
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig(...
772
votes
15
answers
1.8m
views
Changing the tick frequency on the x or y axis
I am trying to fix how python plots my data. Say:
x = [0, 5, 9, 10, 15]
y = [0, 1, 2, 3, 4]
matplotlib.pyplot.plot(x, y)
matplotlib.pyplot.show()
The x axis' ticks are plotted in intervals of 5. Is ...
749
votes
4
answers
1.1m
views
When to use cla(), clf() or close() for clearing a plot
Matplotlib offers these functions:
cla() # Clear axis
clf() # Clear figure
close() # Close a figure window
When should I use each function and what exactly does it do?
687
votes
10
answers
1.7m
views
How to set the axis limits
I need help with setting the limits of y-axis on matplotlib. Here is the code that I tried, unsuccessfully.
import matplotlib.pyplot as plt
plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')...
667
votes
6
answers
1.7m
views
How do I change the figure size with subplots?
How do I increase the figure size for this figure?
This does nothing:
f.figsize(15, 15)
Example code from the link:
import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in ...
636
votes
7
answers
1.8m
views
pyplot scatter plot marker size
In the pyplot document for scatter plot:
matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None,
...
604
votes
13
answers
1.1m
views
Rotate axis tick labels
I can't figure out how to rotate the text on the X Axis. Its a time stamp, so as the number of samples increase, they get closer and closer until they overlap. I'd like to rotate the text 90 degrees ...
590
votes
7
answers
533k
views
What does the argument mean in fig.add_subplot(111)?
Sometimes I come across code such as this:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
fig = plt.figure()
fig.add_subplot(111)
plt.scatter(x, y)
plt.show()
Which ...
586
votes
7
answers
1.4m
views
Plot logarithmic axes
I want to plot a graph with one logarithmic axis using matplotlib.
Sample program:
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)] # exponential
fig = plt.figure()
ax = fig....
563
votes
9
answers
870k
views
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 ...
551
votes
12
answers
851k
views
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 ...
512
votes
1
answer
198k
views
Installation Issue with matplotlib Python [duplicate]
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 (...
511
votes
9
answers
979k
views
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....
503
votes
14
answers
1.3m
views
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 an A4 paper, whose dimensions are 11.7 inches by 8.27 inches in landscape orientation.
502
votes
10
answers
1.2m
views
How to change tick label font size
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?
501
votes
7
answers
1.4m
views
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(length, ...
499
votes
13
answers
751k
views
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 ...
496
votes
31
answers
572k
views
"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:...
491
votes
6
answers
889k
views
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 ...
490
votes
17
answers
977k
views
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(...
489
votes
9
answers
545k
views
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 ...
475
votes
11
answers
465k
views
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
...
468
votes
6
answers
528k
views
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.....
466
votes
10
answers
984k
views
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 ...
444
votes
9
answers
751k
views
Scatter plot with different text at each data point
I am trying to make a scatter plot and annotate data points with different numbers from a list.
So, for example, I want to plot y vs x and annotate with corresponding numbers from n.
y = [2.56422, 3....
427
votes
11
answers
614k
views
How to invert the x or y axis
I have a scatter plot graph with a bunch of random x, y coordinates. Currently the Y-Axis starts at 0 and goes up to the max value. I would like the Y-Axis to start at the max value and go up to 0.
...
419
votes
10
answers
516k
views
How to make inline plots in Jupyter Notebook larger? [duplicate]
I have made my plots inline on my Ipython Notebook with "%matplotlib inline."
Now, the plot appears. However, it is very small. Is there a way to make it appear larger using either notebook ...
414
votes
7
answers
323k
views
Reverse colormap in matplotlib
I would like to know how to simply reverse the color order of a given colormap in order to use it with plot_surface.
407
votes
7
answers
553k
views
Matplotlib different size subplots
I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to ...
377
votes
9
answers
690k
views
How to change plot background color?
I am making a scatter plot in matplotlib and need to change the background of the actual plot to black. I know how to change the face color of the plot using:
fig = plt.figure()
fig.patch....
373
votes
19
answers
929k
views
Plot correlation matrix using pandas
I have a data set with huge number of features, so analysing the correlation matrix has become very difficult. I want to plot a correlation matrix which we get using dataframe.corr() function from ...
367
votes
3
answers
425k
views
How to set a single, main title above all the subplots
I am using pyplot. I have 4 subplots. How to set a single, main title above all the subplots? title() sets it above the last subplot.
362
votes
12
answers
696k
views
Plot two histograms on single chart
I created a histogram plot using data from a file and no problem. Now I wanted to superpose data from another file in the same histogram, so I do something like this
n,bins,patchs = ax.hist(mydata1,...
351
votes
7
answers
754k
views
Plot a horizontal line on a given plot
How do I add a horizontal line to an existing plot?
349
votes
13
answers
387k
views
How to have one colorbar for all subplots
I've spent entirely too long researching how to get two subplots to share the same y-axis with a single colorbar shared between the two in Matplotlib.
What was happening was that when I called the ...
348
votes
5
answers
395k
views
Savefig outputs blank image
I am trying to save plots I make using matplotlib; however, the images are saving blank.
Here is my code:
plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.subplot(122)
y = copy.deepcopy(...
346
votes
15
answers
855k
views
How do I plot in real-time in a while loop?
I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn't seem to be working.
I've isolated the problem into this simple ...
339
votes
16
answers
324k
views
Removing white space around a saved image
I need to take an image and save it after some process. The figure looks fine when I display it, but after saving the figure, I got some white space around the saved image. I have tried the 'tight' ...
338
votes
6
answers
660k
views
Named colors in matplotlib
What named colors are available in matplotlib for use in plots? I can find a list on the matplotlib documentation that claims that these are the only names:
b: blue
g: green
r: red
c: cyan
m: ...
336
votes
13
answers
183k
views
Generating a PNG with matplotlib when DISPLAY is undefined
I am trying to use networkx with Python. When I run this program it get this error. Is there anything missing?
#!/usr/bin/env python
import networkx as nx
import matplotlib
import matplotlib.pyplot
...
333
votes
10
answers
365k
views
How do I make a single legend for many subplots?
I am plotting the same type of information, but for different countries, with multiple subplots with Matplotlib. That is, I have nine plots on a 3x3 grid, all with the same for lines (of course, ...
325
votes
11
answers
427k
views
Remove the legend on a matplotlib figure
To add a legend to a matplotlib plot, one simply runs legend().
How to remove a legend from a plot?
(The closest I came to this is to run legend([]) in order to empty the legend from data. But that ...
324
votes
8
answers
408k
views
Seaborn plots not showing up
I'm sure I'm forgetting something very simple, but I cannot get certain plots to work with Seaborn.
If I do:
import seaborn as sns
Then any plots that I create as usual with matplotlib get the ...
317
votes
9
answers
676k
views
Display image as grayscale
I'm trying to display a grayscale image using matplotlib.pyplot.imshow(). My problem is that the grayscale image is displayed as a colormap. I need it to be grayscale because I want to draw on top ...