python - Jupyter matplotlib plot does not respond to setting xlim or to modifications to x-axis labels -
i new juptyer , matplotlib, i'm struggling adjust x-axis range on plots.
here's code:
plt.figure(1) l_dt = dt[dt['datatype'] == 2.0] if len(l_dt['time'].values) > 1: plt.subplot(223) plt.scatter(l_dt['time'].values, l_dt['measurementvalue'].values) plt.yscale('linear') plt.title('carbs (g)') plt.grid(true) l_dt = dt[dt['datatype'] == 3.0] if len(l_dt['time'].values) > 1: plt.subplot(224) plt.scatter(l_dt['time'].values, l_dt['measurementvalue'].values) plt.yscale('linear') plt.title('activity (mins)') plt.grid(true) plt.tight_layout() plt.xlim(min(dt['time']), max(dt['time'])) plt.show() print(min(dt['time'])) print(min(dt['time']))
here's output:
as can see, though log statement shows min date in april 25 , max date of may 9, seeing axes in 1 case go way march despite line in code:
plt.figure(1) plt.xlim(min(dt['time']), max(dt['time']))
here's i've tried
- i tried moving
xlim
line before plotting, has not changed behavior. - i tried moving 'xlim' line before each
plt.subplot
thanks suggestions.
if had guess (which since there's no data play with), understandably expect plt.xlim
set limits of axes.
however, pyplot
interface operates on current axes tracked matplotlib state machine. can tricky , state machine's might disagree on axes current axes.
i modify example use object-oriented interface:
from matplotlib import pyplot fig, axes = pyplot.subplots(nrows=2, ncols=2, figsize=(8, 8), sharex=true) l_dt = dt[dt['datatype'] == 2.0] if len(l_dt['time'].values) > 1: axes[1, 0].scatter('time', 'measurementvalue', data=l_dt) axes[1, 0].set_yscale('linear') axes[1, 0].set_title('carbs (g)') axes[1, 0].grid(true) l_dt = dt[dt['datatype'] == 3.0] if len(l_dt['time'].values) > 1: axes[1, 1].scatter('time', 'measurementvalue', data=l_dt) axes[1, 1].set_yscale('linear') axes[1, 1].set_title('activity (mins)') axes[1, 1].grid(true) fig.tight_layout() pyplot.show()
using sharex=true
when create figure keep x-axes synced across of subplots.
Comments
Post a Comment