matplotlib graph border adjust with some margin automatically -
when draw matplotlib graph, border box there info maybe hide border. wish can draw border bit away valid data. know can ust xlim() , ylim() adjust manually if changed data, should refine again. there have 1 automatically method maintain margin between graph , border.
e.g.
import numpy np import matplotlib.pyplot plt import pandas pd ticks = [ "9-28 11:00:00.234", "9-28 11:11:00.123", "9-28 11:40:00.654", "9-28 11:50:00.341", "9-28 12:00:00.773"] y = np.array([10, 12, 9, 15, 11]) x = [pd.to_datetime(i, format="%m-%d %h:%m:%s.%f") in ticks] plt.step(x,y) plt.xticks(rotation=25) plt.xticks(x, ticks) plt.show() the code output graph below:
the steps has been messed border! wish maintain margin there see info more better!
based on suggestion of comments above, treating timestamp objects, can take argument .value time in nanoseconds. can write autolim() function adjust limits:
def autolim(pxmin=0.05, pxmax=0.05, pymin=0., pymax=0.05): pandas.tslib import timestamp ax = plt.gca() x, y = ax.lines[0].get_data() x = sorted(x) y = sorted(y) dx = x[-1].value - x[0].value dy = y[-1] - y[0] xmin = timestamp(x[0].value - dx*pxmin) xmax = timestamp(x[-1].value + dx*pxmax) ymin = y[0] - dy*pymin ymax = y[-1] + dy*pymax ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) if phone call function before plt.show() line of code should get:
matplotlib
No comments:
Post a Comment