Fill matplotlib subplots by column, not row

2024/10/10 16:19:55

By default, matplotlib subplots are filled by row, not by column. To clarify, the commands

plt.subplot(nrows=3, ncols=2, idx=2)
plt.subplot(nrows=3, ncols=2, idx=3)

first plot into the upper right plot of the 3x2 plot grid (idx=2), and then into the middle left plot (idx=3).

Sometimes it may be desirable for whatever reason to fill the subplots by row, not by column (for example, because directly consecutive plots belong together and are easier interpretable when positioned below each other, rather than next to each other). How can this be achieved?

Answer

You can create the 3x2 array of axes using:

fig, axes = plt.subplots(nrows=3, ncols=2)

If you transpose this array, then flatten you can plot column wise, rather than row wise:

fig, axes = plt.subplots(nrows=3, ncols=2)for ax in axes.T.flatten():ax.plot([1,2,3])
https://en.xdnf.cn/q/69874.html

Related Q&A

Find the 2nd highest element

In a given array how to find the 2nd, 3rd, 4th, or 5th values? Also if we use themax() function in python what is the order of complexity i.e, associated with this function max()?.def nth_largest(…

pandas data frame - select rows and clear memory?

I have a large pandas dataframe (size = 3 GB):x = read.table(big_table.txt, sep=\t, header=0, index_col=0)Because Im working under memory constraints, I subset the dataframe:rows = calculate_rows() # a…

How do I format a websocket request?

Im trying to create an application in Python that powers a GPIO port when the balance of a Dogecoin address changes. Im using the websocket API here and this websocket client.My code looks like this:fr…

cherrypy and wxpython

Im trying to make a cherrypy application with a wxpython ui. The problem is both libraries use closed loop event handlers. Is there a way for this to work? If I have the wx ui start cherrypy is that g…

What is the logic behind d3.js nice() ticks

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.var s = d3.scale.linear().domain([minValue, maxValue]); var ticks = …

Changing iterable variable during loop

Let it be an iterable element in python. In what cases is a change of it inside a loop over it reflected? Or more straightforward: When does something like this work?it = range(6) for i in it:it.remo…

Are CPython, IronPython, Jython scripts compatible with each other?

I am pretty sure that python scripts will work in all three, but I want to make sure. I have read here and there about editors that can write CPython, Jython, IronPython and I am hoping that I am look…

Python. Print mac address out of 6 byte string

I have mac address in 6 byte string. How would you print it in "human" readable format?Thanks

Cursors with postgres, where is the data stored and how many calls to the DB

Hi I am using psycopg2 for postgres access.I am trying to understand where "cursor" stores the returned rows. Does it store it in the database as a temporary table or is it on the clients en…

Django - How to allow only the owner of a new post to edit or delete the post?

I will be really grateful if anyone can help to resolve the issue below. I have the following Django project coding. The problem is: when the browser was given "/posts/remove/<post_id>/"…