0

Using matlotlib, I can create figures that look like this:

enter image description here

Here, each row consists of a series of numbers from 0 to 0.6. The left hand axis text indicates the maximum value in each row. The bottom axis text represents the column indices.

The code for the actual grid essentially involves this line:

    im = ax[r,c].imshow(info_to_use, vmin=0, vmax=0.6, cmap='gray')

where ax[r,c] is the current subplot axes at row r and column c, and info_to_use is a numpy array of shape (num_rows, num_cols) and has values between 0 and 0.6.

I am wondering if there is a way to convert the code above so that it instead displays bar charts, one per row? Something like this hand-drawn figure:

enter image description here

(The number of columns is not the same in my hand-drawn figure compared to the earlier one.) I know this would result in a very hard-to-read plot if it were embedded into a plot like the first one here. I would have this for a plot with fewer rows, which would make the bars easier to read.

The references that helped me make the first plot above were mostly from:

But I'm not sure how to make the jump from these to a bar chart in each row. Or at least something that could mirror it, e.g., instead of shading the full cell gray, only shade as much of it based on the percentage of the vmax?

ComputerScientist
  • 936
  • 4
  • 12
  • 20
  • `a bar chart in each row` .. If row refers to `a series of numbers from 0 to 0.6`, then you just need to make a bar chart for each row. The Matplotlib Gallery has many examples of [bar charts](https://matplotlib.org/gallery/index.html#lines-bars-and-markers) and you probably want to put each in a [subplot](https://matplotlib.org/gallery/index.html#subplots-axes-and-figures). I recommend spending some time with [the tutorial](https://matplotlib.org/gallery/index.html#subplots-axes-and-figures). Try to get through at least the first three and the Artist tutorial in the intermediate section. – wwii Aug 25 '19 at 01:46
  • Iterate over each row of `info_to_use` using enumerate (start at one) to keep track of the row number; in the loop call plt.subplot with n_rows, one column and the rownumber; then call plt.bar with the row itself. – wwii Aug 25 '19 at 02:13
  • Possible duplicate: [Bar chart matplotlib based on array of 8 rows with 5 values each](https://stackoverflow.com/questions/43445509/bar-chart-matplotlib-based-on-array-of-8-rows-with-5-values-each) – wwii Aug 25 '19 at 02:16
  • @wwii Thanks for the links to the tutorials. I might need to do a nested subplot https://stackoverflow.com/questions/34933905/matplotlib-adding-subplots-to-a-subplot which added another source of confusion, but maybe it's best for me to make all these figures individually and paste them together using photoshop or google drawings – ComputerScientist Aug 25 '19 at 02:26
  • @wwii The stacked bar chart in your 'possible duplicate' might do the trick, *except* I would probably add some whitespace so that each row begins at the same x axis level. – ComputerScientist Aug 25 '19 at 02:27
  • [Can I answer my own question?](https://stackoverflow.com/help/self-answer). – wwii Aug 25 '19 at 14:26

1 Answers1

1
import numpy as np
from matplotlib import pyplot as plt

a = np.random.rand(10,20)*.6

In a loop, call plt.subplot then plt.bar for each row in the 2-d array.

for i, thing in enumerate(a,1):
    plt.subplot(a.shape[0],1,i)
    plt.bar(range(a.shape[1]),thing)
plt.show()
plt.close()

Or, create all the subplots; then in a loop make a bar plot with each Axes.

fig, axes = plt.subplots(a.shape[0],1,sharex=True)
for ax, data in zip(axes, a):
    ax.bar(range(a.shape[1]), data)
plt.show()
plt.close()
wwii
  • 23,232
  • 7
  • 37
  • 77