I recently had a problem with aliasing when plotting high-density data using the matplotlib pyplot bar function. Chunks of data appear missing in the plot despite actually being there. The below example takes around 40 seconds to run on my medium spec laptop.
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(1,3)
ax1 = axes[0]
ax2 = axes[1]
ax3 = axes[2]
#Get some high-density data
raw=np.abs(np.random.normal(10000,1000,2000000).astype(np.int16))
counts=np.bincount(raw)
bins=np.arange(len(counts))
#Three different ways to plot the histogram
ax1.bar(bins, counts)
ax2.bar(bins, counts, width=1)
ax3.fill_between(bins, counts, step='mid') #Setting step='mid' centres the bars on the data
#Tidy up and plot
ax3.set_ylim(bottom=0)
for ax in axes: ax.set_xlim(5000, 15000)
ax1.set_title("plt.bar")
ax2.set_title("plt.bar, width=1")
ax3.set_title("plt.fill_between, step='mid'")
fig.tight_layout()
plt.savefig('norm.png', dpi=600)
plt.savefig("norm.svg",format='svg') #vector graphics means all data is represented in the image
plt.show()
matplotlib pyplot bar showing aliasing
After a little digging, I found possible solutions and will self-answer this question.
I am hoping for some accurate way of displaying the data.