I have to make a vector plot and I want to just see the vectors without the axes, titles etc so here is how I try to do it:
pyplot.figure(None, figsize=(10, 16), dpi=100)
pyplot.quiver(data['x'], data['y'], data['u'], data['v'],
pivot='tail',
units='dots',
scale=0.2,
color='black')
pyplot.autoscale(tight=True)
pyplot.axis('off')
ax = pyplot.gca()
ax.xaxis.set_major_locator(pylab.NullLocator())
ax.yaxis.set_major_locator(pylab.NullLocator())
pyplot.savefig("test.png",
bbox_inches='tight',
transparent=True,
pad_inches=0)
and despite my efforts to have an image 1000 by 1600 I get one 775 by 1280. How do I make it the desired size? Thank you.
UPDATE The presented solution works, except in my case I also had to manually set the axes limits. Otherwise, matplotlib could not figure out the "tight" bounding box.
The problem you are having is that bbox_inches='tight' just removes all of the extra white space around your figure, it does not actually re-arrange anything in your figure, after it has been rendered. You might need to tweak the parameters you pass to tight_layout (tutorial) to get your desired effect.
If you've already got the figure created, say it's 'figure 1' (that's the default one when you're using pyplot), you can use figure(num=1, figsize=(8, 6), ...) to change it's size etc. If you're using pyplot/pylab and show() to create a popup window, you need to call figure(num=1,...)
To save the file in pdf format, use savefig() method where the image name is myImagePDF. pdf, format="pdf". We can set the dpi value to get a high-quality image. Using the saving() method, we can save the image with format=”png” and dpi=1200.
import matplotlib.pyplot as plt
import numpy as np
sin, cos = np.sin, np.cos
fig = plt.figure(frameon = False)
fig.set_size_inches(5, 8)
ax = plt.Axes(fig, [0., 0., 1., 1.], )
ax.set_axis_off()
fig.add_axes(ax)
x = np.linspace(-4, 4, 20)
y = np.linspace(-4, 4, 20)
X, Y = np.meshgrid(x, y)
deg = np.arctan(Y**3-3*Y-X)
plt.quiver(X, Y, cos(deg), sin(deg), pivot='tail', units='dots', color='red')
plt.savefig('/tmp/test.png', dpi=200)
yields
You can make the resultant image 1000x1600 pixels by setting the figure to be 5x8 inches
fig.set_size_inches(5, 8)
and saving with DPI=200
:
plt.savefig('/tmp/test.png', dpi=200)
The code to remove the border was taken from here.
(The image posted above is not to scale since 1000x1600 is rather large).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With