python - How can I change the x axis in matplotlib so there is no white space? -
so learning how import data , work in matplotlib , having trouble tho have exact code book.
this plot looks like, question how can there no white space between start , end of x-axis.
here code:
import csv matplotlib import pyplot plt datetime import datetime # dates , high temperatures file. filename = 'sitka_weather_07-2014.csv' open(filename) f: reader = csv.reader(f) header_row = next(reader) #for index, column_header in enumerate(header_row): #print(index, column_header) dates, highs = [], [] row in reader: current_date = datetime.strptime(row[0], "%y-%m-%d") dates.append(current_date) high = int(row[1]) highs.append(high) # plot data. fig = plt.figure(dpi=128, figsize=(10,6)) plt.plot(dates, highs, c='red') # format plot. plt.title("daily high temperatures, july 2014", fontsize=24) plt.xlabel('', fontsize=16) fig.autofmt_xdate() plt.ylabel("temperature (f)", fontsize=16) plt.tick_params(axis='both', which='major', labelsize=16) plt.show()
in matplotlib 2.x there automatic margin set @ edges, ensures data nicely fitting within axis spines. in case such margin desired on y axis. default set 0.05
in units of axis span. set margin 0
on x axis, use
plt.margins(x=0)
or
ax.margins(x=0)
depending on context. see the documentation.
in case want rid of margin in whole script, can use
plt.rcparams['axes.xmargin'] = 0
at beginning of script (same y
of course). if want rid of margin entirely , forever, might want change according line in matplotlib rc file.
alternatively changing margins, use
plt.xlim(..)
or ax.set_xlim(..)
manually set limits of axes such there no white space left. wiki
Comments
Post a Comment