【发布时间】:2018-10-01 01:24:32
【问题描述】:
基本上,我想将 3 个图堆叠在一起,以便它们可以共享 x。问题是,有些图使用 twinx 命令(?),因为它们有 2 个或更多 y 轴。
如果不清楚我的意思,我已经手动将图表放在一起以查看它的外观,但它并不那么好。 (注意/忽略图表下方的线条,我也希望 1 或 2 上没有 x 标签)
此外,下面显示了其中一个数字代码的示例 - 如果它有帮助的话。
fig = plt.figure()
ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2, colspan=2)
fig.subplots_adjust(left=0.13, bottom=0.12, right=0.91, top=0.9, wspace=0.3,
hspace=0.5)
plt.margins(x=0)
ax.scatter(data['elapsed_seconds'], Abut_Stress, label='Abutment Stress
Calculated', linewidth=0, marker='o', s=20,
c='k')
ax.plot(data['elapsed_seconds'], data['wheel_temp_c'], label='Wheel
Temperature', linestyle="-", color='b')
ax.plot(data['elapsed_seconds'], data['abut_temp_c'], label='Abutment
Temperature', linestyle="-", color='g')
ax2 = ax.twinx()
ax2.plot(data['elapsed_seconds'], data['wheel_speed'], label='Wheel Speed',
linestyle="-", color='r')
# -------------Get legend names to be plotted in the same legend box-------
h1, l1 = ax.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
lg = ax.legend(h1 + h2, l1 + l2, bbox_to_anchor=(0., 0.8, 1., .102), loc=3,
ncol=1, borderaxespad=0., fontsize=28, )
lg.get_frame().set_linewidth(0.5)
# ----------------------Settings for the first figure window---------------
ax.set_ylim(0, 1000)
ax2.set_ylim(0, 12)
ax.set_xlim(0,data_end)
ax.set_xlabel(r"Time (s)")
ax.set_ylabel(r"Abutment Stress (MPa) Temperature($^\circ$C)")
ax2.set_ylabel(r"Wheel Speed (RPM)")
ax.get_xaxis().tick_bottom()
ax2.get_xaxis().tick_bottom()
【问题讨论】:
-
那么当您创建 3 个单独的图形时,您希望它们全部在 1 个图形中,共享 x 轴?
-
@DavidG 没错!
标签: python python-2.7 matplotlib graph