【问题标题】:What is subplot_adjust() doing to pyplot axes?subplot_adjust() 对 pyplot 轴做了什么?
【发布时间】:2015-02-06 15:51:48
【问题描述】:

我正在尝试在pyplot 图中绘制多个子图,其中一个有两个子图。我通过根据底部的位置创建一个额外的pyplot.ax 来处理这个问题。

现在,当我使用 fig.subplots_adjust() 调整轴 1 到 4 时出现问题,以便在右侧为图例留出额外的空间。在下图中,您可以看到虽然我的两个数据集长度相同,但条形图向右延伸得更远。

我想对ax5 应用与使用fig.subplot_adjust() 时对其他四个轴相同的调整,但我无法弄清楚这种方法对 matplotlib.axes.Axes 实例。

查看文档,我找不到适合我目的的方法: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes

那么fig.subplot_adjust() 对我的斧头做了什么?如何重现此行为以使所有轴对齐?

import numpy as np
import matplotlib.pyplot as plt
import datetime

fig, ( ax1, ax2, ax3 , ax4) = plt.subplots( figsize=(18.0, 11.0) , nrows=4, ncols=1) 

## some fake stand-alone data
days = 365 * 5
dates = [datetime.datetime(2000, 1, 1, 0, 0) + datetime.timedelta( day - 1) for day in range(days)]
data_series = np.random.rand( days )
data_series2 = [np.sin(x * 2 * np.pi / 365 ) + np.random.rand(1) * 0.1 for x in range( days ) ]

######  Plots made up temperatures 
ax4.set_frame_on(False)
ax4.plot_date( dates , data_series2 , color="black", ls="solid", lw=2, ms=0 )

# Now on the same plot try to add som precipitation as a plot
ax5 = fig.add_axes(ax4.get_position() , frameon=True, zorder = -10.0)
ax5.bar( dates, data_series, edgecolor="blue", zorder = -10.0 )
ax5.xaxis_date()

# gets rid of bar-plot labels
ax5.set_xticks([]); ax5.set_yticks([])

fig.subplots_adjust(right=0.8) # <- Pandora's box
plt.show()

【问题讨论】:

  • 你为什么要在同一块土地上制作两个轴(ax4 和 ax5)?您可以在同一轴上放置多个图...
  • 谢谢@Ajean,那我可以用不同的 y 轴绘制它吗?在这种情况下,我希望温度和降水在同一个子图上,数据具有不同的数量级,我希望条形的零位于底部,而图会在它周围发生变化。如果您认为我可以在同一把斧头上做到这一点,我愿意研究一下。
  • 我看到您已经有了答案,但是另一种方法也可以满足您的需求,可以回答您的最后一个问题,是的,您可以使用不同的 y 轴进行绘图。请参阅有关孪生轴的教程here
  • @Ajean ,我最终选择了您建议的解决方案,它比我尝试做的更简洁和优雅。不过,Data_addict 擅长解释示例中的故障。谢谢你们俩。

标签: python-2.7 matplotlib subplot


【解决方案1】:

这里的问题是 ax5 不在子图中。

fig.get_axes()

[<matplotlib.axes._subplots.AxesSubplot at 0x220175c0>,
<matplotlib.axes._subplots.AxesSubplot at 0x18d48240>,
<matplotlib.axes._subplots.AxesSubplot at 0x1c5f3630>,
<matplotlib.axes._subplots.AxesSubplot at 0x1a430710>,
<matplotlib.axes._axes.Axes at 0x1c4defd0>] # There is ax5 and it is not under _subplots

所以当你这样做时

fig.subplots_adjust(right=0.8)

您调整子图而不是直接调整 ax,这样您就不会影响 ax5。

更简单的方法是在调用ax5之前调整ax4,因此ax5将与ax4具有相同的比例。

通过调用

fig.subplots_adjust(right=0.8)

之前

ax5 = fig.add_axes(ax4.get_position() , frameon=True, zorder = -10.0)

你会得到正确的输出。

所以你的代码必须是这样的;

import numpy as np
import matplotlib.pyplot as plt
import datetime

fig, ( ax1, ax2, ax3 , ax4) = plt.subplots( figsize=(18.0, 11.0) , nrows=4, ncols=1) 

## some fake stand-alone data
days = 365 * 5
dates = [datetime.datetime(2000, 1, 1, 0, 0) + datetime.timedelta( day - 1) for day in range(days)]
data_series = np.random.rand( days )
data_series2 = [np.sin(x * 2 * np.pi / 365 ) + np.random.rand(1) * 0.1 for x in range( days ) ]

######  Plots made up temperatures 
ax4.set_frame_on(False)
ax4.plot_date( dates , data_series2 , color="black", ls="solid", lw=2, ms=0 )

# I move the subplot_adjust here before you create ax5
fig.subplots_adjust(right=0.8) 

# Now on the same plot try to add som precipitation as a plot
ax5 = fig.add_axes(ax4.get_position() , frameon=True, zorder = -10.0)
ax5.bar( dates, data_series, edgecolor="blue", zorder = -10.0 )
ax5.xaxis_date()

# gets rid of bar-plot labels
ax5.set_xticks([]); ax5.set_yticks([])

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2010-11-07
    • 2017-01-20
    • 2013-03-07
    相关资源
    最近更新 更多