I'm making some pretty big graphs, and the whitespace in the border is taking up a lot of pixels that would be better used by data. It seems that the border grows as the graph grows.
Here are the guts of my graphing code:
import matplotlib
from pylab import figure
fig = figure()
ax = fig.add_subplot(111)
ax.plot_date((dates, dates), (highs, lows), '-', color='black')
ax.plot_date(dates, closes, '-', marker='_', color='black')
ax.set_title('Title')
ax.grid(True)
fig.set_figheight(96)
fig.set_figwidth(24)
Is there a way to reduce the size of the border? Maybe a setting somewhere that would allow me to keep the border at a constant 2 inches or so?
Import matplotlib. To change the figure size, use figsize argument and set the width and the height of the plot. Next, we define the data coordinates. To plot a bar chart, use the bar() function. To display the chart, use the show() function.
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,...)
figsize is a tuple of the width and height of the figure in inches, and dpi is the dots-per-inch (pixel per inch).
Since it looks like you're just using a single subplot, you may want to skip add_subplot
and go straight to add_axes
. This will allow you to give the size of the axes (in figure-relative coordinates), so you can make it as large as you want within the figure. In your case, this would mean your code would look something like
import matplotlib.pyplot as plt
fig = plt.figure()
# add_axes takes [left, bottom, width, height]
border_width = 0.05
ax_size = [0+border_width, 0+border_width,
1-2*border_width, 1-2*border-width]
ax = fig.add_axes(ax_size)
ax.plot_date((dates, dates), (highs, lows), '-', color='black')
ax.plot_date(dates, closes, '-', marker='_', color='black')
ax.set_title('Title')
ax.grid(True)
fig.set_figheight(96)
fig.set_figwidth(24)
If you wanted, you could even put the parameters to set_figheight
/set_figwidth
directly in the figure()
call.
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