How do I draw a grid onto a plot in Python?

You want to use pyplot.grid: x = numpy.arange(0, 1, 0.05) y = numpy.power(x, 2) fig = plt.figure() ax = fig.gca() ax.set_xticks(numpy.arange(0, 1, 0.1)) ax.set_yticks(numpy.arange(0, 1., 0.1)) plt.scatter(x, y) plt.grid() plt.show() ax.xaxis.grid and ax.yaxis.grid can control grid lines properties.

How to do a scatter plot with empty circles in Python?

From the documentation for scatter: Optional kwargs control the Collection properties; in particular: edgecolors: The string ‘none’ to plot faces with no outlines facecolors: The string ‘none’ to plot unfilled outlines Try the following: import matplotlib.pyplot as plt import numpy as np x = np.random.randn(60) y = np.random.randn(60) plt.scatter(x, y, s=80, facecolors=”none”, edgecolors=”r”) plt.show() Note: … Read more

Generate a heatmap using a scatter data set

If you don’t want hexagons, you can use numpy’s histogram2d function: import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test data x = np.random.randn(8873) y = np.random.randn(8873) heatmap, xedges, yedges = np.histogram2d(x, y, bins=50) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] plt.clf() plt.imshow(heatmap.T, extent=extent, origin=’lower’) plt.show() This makes a 50×50 heatmap. … Read more

How to remove frame from matplotlib (pyplot.figure vs matplotlib.figure ) (frameon=False Problematic in matplotlib)

ax.axis(‘off’), will as Joe Kington pointed out, remove everything except the plotted line. For those wanting to only remove the frame (border), and keep labels, tickers etc, one can do that by accessing the spines object on the axis. Given an axis object ax, the following should remove borders on all four sides: ax.spines[‘top’].set_visible(False) ax.spines[‘right’].set_visible(False) … Read more

plot a circle with pyplot

You need to add it to an axes. A Circle is a subclass of an Patch, and an axes has an add_patch method. (You can also use add_artist but it’s not recommended.) Here’s an example of doing this: import matplotlib.pyplot as plt circle1 = plt.Circle((0, 0), 0.2, color=”r”) circle2 = plt.Circle((0.5, 0.5), 0.2, color=”blue”) circle3 … Read more

_tkinter.TclError: no display name and no $DISPLAY environment variable

Matplotlib chooses Xwindows backend by default. You need to set matplotlib to not use the Xwindows backend. Add this code to the start of your script (before importing pyplot) and try again: import matplotlib matplotlib.use(‘Agg’) Or add to .config/matplotlib/matplotlibrc line backend: Agg to use non-interactive backend. echo “backend: Agg” > ~/.config/matplotlib/matplotlibrc Or when connect to … Read more

Getting individual colors from a color map in matplotlib

You can do this with the code below, and the code in your question was actually very close to what you needed, all you have to do is call the cmap object you have. import matplotlib cmap = matplotlib.cm.get_cmap(‘Spectral’) rgba = cmap(0.5) print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0) For values outside of the range [0.0, … Read more

import module from string variable

The __import__ function can be a bit hard to understand. If you change i = __import__(‘matplotlib.text’) to i = __import__(‘matplotlib.text’, fromlist=[”]) then i will refer to matplotlib.text. In Python 2.7 and Python 3.1 or later, you can use importlib: import importlib i = importlib.import_module(“matplotlib.text”) Some notes If you’re trying to import something from a sub-folder … Read more

Common xlabel/ylabel for matplotlib subplots

This looks like what you actually want. It applies the same approach of this answer to your specific case: import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(6, 6)) fig.text(0.5, 0.04, ‘common X’, ha=”center”) fig.text(0.04, 0.5, ‘common Y’, va=”center”, rotation=’vertical’)