[英]Converting multi-dimensional sublots
我正在运行一个片段,它适用于两个数据范围(fig, ax = plt.subplots(5, 2, facecolor='#eeeeee'),但是,一旦我试图将它限制在一个范围内(对于 j在范围(1))中,plot 的右侧填充了空子图。尝试使用 flatten() function 来减小尺寸,但没有帮助。我的代码是:
from matplotlib.cm import tab10
from matplotlib.ticker import EngFormatter
fig, ax = plt.subplots(5, 2, facecolor='#eeeeee')
fig.set_size_inches(17,20)
countries = [' - PL']
pages_df = [phrases]
for i in range(5):
for j in range(1):
ax[i, j].set_frame_on(False)
ax[i, j].barh((pages_df[j][pages_df[j]['searchTerms'] == tickers[i]]
.sort_values('totalResults')['displayLink']
.str.replace('www.', '')),
(pages_df[j][pages_df[j]['searchTerms'] == tickers[i]]
.sort_values('totalResults')['totalResults']),
color=tab10.colors[i + 5 * j])
ax[i, j].grid(axis='x')
ax[i, j].set_title('Pages per domain. Keyword: "' + tickers[i] + '"' + countries[j],
fontsize=9)
ax[i, j].tick_params(labelsize=10)
ax[i, j].xaxis.set_major_formatter(EngFormatter())
plt.tight_layout()
plt.autoscale()
fig.savefig('Pages per domain' + '.png',
facecolor='#eeeeee', dpi=120, bbox_inches="tight")
plt.show()
任何关于如何摆脱与第二个空白数据范围相关的空子图的线索将不胜感激。
您明确定义使用 5 行和 2 列:
fig, ax = plt.subplots(5, 2, facecolor='#eeeeee')
只需使用行(参见plt.subplots
的文档):
fig, ax = plt.subplots(nrows=5, facecolor='#eeeeee')
然后删除你的内部循环并用一个切片引用斧头。
for i in range(5):
ax[i].set_frame_on(False)
# rest of code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.