Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

python - new pythonic style for shared axes square subplots in matplotlib?

Related to: plotting autoscaled subplots with fixed limits in matplotlib

I would like to make a set of subplots that are all on the same scale, using the subplots new compact style, as in http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots and have them be square.

I tried:

fig, axes = subplots(numplots, 1, sharex=True, sharey=True, adjustable='box', aspect='equal')

But I found that these keyword arguments are not implemented in the subplots wrapper. What's the way to do it?

To reiterate, the goal is simply to have shared axes, so that all the data are on the same scale, and have the plots be square.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use adjustable='box-forced' instead of adjustable='box'.

As @cronos mentions, you can pass it in using the subplot_kw kwarg (additional keyword arguments to subplots are passed on to the Figure not the Axes, thus the need for subplot_kw).

Instead, I'm going to use setp, which basically just does for item in sequence: item.set(**kwargs). (All matplotlib artists have a set method that can be used similar to matlab's set.)

Which one is the "better" approach will depend on what you're doing. A lot of people would argue that setp is very "unpythonic", but I don't see the problem with it.

As a quick example:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=3, sharex=True, sharey=True)
plt.setp(axes.flat, aspect=1.0, adjustable='box-forced')

axes[0].plot(range(50))

plt.show()

enter image description here

I forget the reason for the two different adjustable box styles, at the moment. I remember that I found it really confusing the first time I came across it, and I dug through the code and there was some obvious reason for it... I can't remember what that reason was at the moment, though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...