user7846046
user7846046

Reputation:

Pyplot: Contour lines that enclose a certain number of points (or a certain probability value) in a scatter plot?

I want to create a PDF from a scatter plot. This is the code I'm currently using:

data = np.column_stack((x, y))
nbins = 100
xmin = -0.5; xmax = 0.5; ymin = -5; ymax = 5
k = kde.gaussian_kde(data.T)
xi, yi = np.mgrid[xmin:xmax:nbins*1j, ymin:ymax:nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
fig, ax = plt.subplots(figsize=(5.5,4))
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='gouraud', cmap='Purples')
plt.contour(xi, yi, zi.reshape(xi.shape), levels = [0.2, 0.5, 0.7, 1, 1.5], linewidths=1, linestyles='dashed', cmap='viridis')
plt.xlabel('$x$')
plt.ylabel('$y$]')
plt.grid(True, linestyle='--')
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
fig.savefig('test.png', dpi=600)
plt.close(fig)

And this is the plot I'm getting from the code:

enter image description here

I have two questions about this:

Data: x and y. (Format: npy.)

Upvotes: 0

Views: 1306

Answers (1)

user16673478
user16673478

Reputation: 1

You can use:

plt.savefig('Testing.pdf', format='pdf')

Upvotes: 0

Related Questions