Histogram for discrete values with matplotlib

Given the title of your question, I will assume that the discretization size is constant. You can find this discretization size (or at least, strictly, n times that size as you may not have two adjacent samples in your data) np.diff(np.unique(data)).min() This finds the unique values in your data (np.unique), finds the differences between then … Read more

Suggestions to plot overlapping lines in matplotlib?

I have the same issue on a plot with a high degree of discretization. Here the starting situation: import matplotlib.pyplot as plt grid=[x for x in range(10)] graphs=[ [1,1,1,4,4,4,3,5,6,0], [1,1,1,5,5,5,3,5,6,0], [1,1,1,0,0,3,3,2,4,0], [1,2,4,4,3,2,3,2,4,0], [1,2,3,3,4,4,3,2,6,0], [1,1,3,3,0,3,3,5,4,3], ] for gg,graph in enumerate(graphs): plt.plot(grid,graph,label=”g”+str(gg)) plt.legend(loc=3,bbox_to_anchor=(1,0)) plt.show() No one can say where the green and blue lines run exactly and … Read more

Removing frame while keeping axes in pyplot subplots

If you want to remove the axis spines, but not the other information (ticks, labels, etc.), you can do that like so: fig, ax = plt.subplots(7,1, sharex=True) t = np.arange(0, 1, 0.01) for i, a in enumerate(ax): a.plot(t, np.sin((i + 1) * 2 * np.pi * t)) a.spines[“top”].set_visible(False) a.spines[“right”].set_visible(False) a.spines[“bottom”].set_visible(False) or, more easily, using seaborn: … Read more

Can I give a border (outline) to a line in matplotlib plot function?

If you plot a line twice it won’t show up in the legend. It’s indeed better to use patheffects. Here are two simple examples: import matplotlib.pyplot as plt import numpy as np import matplotlib.patheffects as pe # setup data x = np.arange(0.0, 1.0, 0.01) y = np.sin(2*2*np.pi*t) # create line plot including an outline (stroke) … Read more

How to plot cdf in matplotlib in Python?

I know I’m late to the party. But, there is a simpler way if you just want the cdf for your plot and not for future calculations: plt.hist(put_data_here, normed=True, cumulative=True, label=”CDF”, histtype=”step”, alpha=0.8, color=”k”) As an example, plt.hist(dataset, bins=bins, normed=True, cumulative=True, label=”CDF DATA”, histtype=”step”, alpha=0.55, color=”purple”) # bins and (lognormal / normal) datasets are pre-defined … Read more

Barplot savefig() returning an AttributeError

I solved the issue by changing ax.savefig(‘file.png’) to ax.figure.savefig(‘file.png’) I guess accessing the figure directly is one way to get to the savefig() method for the barplot. @WoodChopper also has a working solution, but it requires another import statement, and utilizing pyplot’s savefig() directly. Either solution does require setting matplotlib.use(‘Agg’) to get around the DISPLAY … Read more

What is the difference between set_xlim and set_xbound?

The bound can changes automatically, if you later plot something that is not within the bound. In contrast, limits are fixed and do not change automatically. import pylab as p t = p.arange(0.0, 2.0, 0.01) s = p.sin(2*p.pi*t) ax=p.subplot(111) ax.plot(t, s, color=”r”,linewidth=1.0) ax.set_ylim(-1,1) ax.plot(t, s+1, color=”g”,linewidth=1.0, label=”Graph2″) p.show() ax=p.subplot(111) ax.plot(t, s, color=”r”,linewidth=1.0) ax.set_ybound(-1,1) ax.plot(t, s+1, … Read more