我正在使用matplotlib绘制图表。 我想在图中放置图例,因此我使用ax.set_position()来缩小图宽。 但是,这仅在省略fig.autofmt_xdate()时有效 要有45度旋转的x标签(时间戳)。 有办法做到这两点吗?这是一个代码示例。
User::with('bookings')->where('starts_at' , '>' time())->get();
答案 0 :(得分:1)
fig.autofmt_xdate()
会自动调用subplot_adjust()
,这会影响您的布局。
解决方案很简单:只需执行与fig.autofmt_xdate()
相同的步骤,但手动执行。
from matplotlib import gridspec
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
fig = plt.figure(figsize=(13,13))
gs = gridspec.GridSpec(nrows=3, ncols=1, height_ratios=[3,1,1])
for i in xrange (0,3):
ax = fig.add_subplot(gs[i])
ax.scatter(np.random.random((10)),np.random.random((10)), label='scatter %d'%(i))
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.5, box.height])
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
#fig.autofmt_xdate()
for ax in fig.get_axes():
if ax.is_last_row():
for label in ax.get_xticklabels():
label.set_ha('right')
label.set_rotation(30.)
else:
for label in ax.get_xticklabels():
label.set_visible(False)
ax.set_xlabel('')
plt.show()