Adding offset to xy axes in matplotlib

I am writing a view in django that sends a PDF file as an answer. Pdf has a linear graph of values ​​by dates made using matplotlib. The graph is very similar to this one one.

If I have a maximum y-data point like 1000, then the graph touches the top x axis, which I don't want. For example, see this figure.

enter image description here.

In (3,0,4,0), the line graph touches the top axis of the axis.

I want to allocate extra space for yaxis (e.g. increase max-y to 1100) so that my graph looks good, as shown in the first image.

How can I do it?

from matplotlib.backends.backend_pdf import FigureCanvasPdf
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter
r_name  = request.GET.get('r_name')      
index_id = request.GET.get('index_id')
resp = api_views.historical_observations(request,index_id)      
data = resp.data.get('data')    
x,y=[],[] 
for datam in data:
    x.append(datam.get('measurement_date'))
    y.append(datam.get('value'))        
fig=Figure()
ax=fig.add_subplot(111)   
ax.set_title(r_name)
ax.plot_date(x, y, '-bo')    
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))    
fig.autofmt_xdate()
canvas=FigureCanvasPdf(fig)
response= HttpResponse(content_type='application/pdf')
filename = settings.PDF_FILE_PREFIX+"_"+r_name+"_"+request.GET.get('start_date')+"_" +\
            request.GET.get('end_date')+".pdf"
response['Content-Disposition'] = "attachment; filename="+filename
canvas.print_pdf(response)
return response
+4
source share
1 answer

: ax.set_ymargin(0.1) ( float 0 1)

ax.set_ylim([min(y)-1, max(Y)+1])

1 , . , . ylim , 0,9\1,1

+3

All Articles