For loop to read stacked image

Dear All,

How could I read the stacked file *.tif separately at once, instead of one by one as below! , using for loop,

Or, How to convert the following code to for loop?

import rasterio
from rasterio.plot  import show
from matplotlib  import pyplot
import matplotlib.pyplot as plt

  

# to display bands one by one
dataset  = rasterio. open ( 'D:\data_stacked_2015.tif' )

fig, (axes1, axes2, axes3)  = pyplot.subplots( 1 , 3 , figsize = ( 21 , 7 ))
show((dataset,  64 ), ax  = axes1, cmap = 'gray' , title = 'red channel' )
show((dataset,  65 ), ax  = axes2, cmap = 'gray' , title = 'green channel' )
show((dataset,  66 ), ax  = axes3, cmap = 'gray' , title = 'blue channel' )

for ax  in axes1, axes2, axes3:
     ax.axis( 'off' )

plt.show()

Welcome, falahfakhri.

Considering how important indentation is in Python, I suggest you edit your post with the following:

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Almost there, but it is still difficult to read, because none of the visible backticks should be there:
Instead of this:

`import` `rasterio`

`from` `rasterio.plot ` `import` `show`

`from` `matplotlib ` `import` `pyplot`

`import` `matplotlib.pyplot as plt`

Better to remove the backticks and unnecessary spaces:

import rasterio
from rasterio.plot  import show
from matplotlib  import pyplot
import matplotlib.pyplot as plt

Many campers help by copying the code, and running it in their environment. This is difficult if the code is not formatted correctly, and you are less likely to get help with messy code.

Dear @Sky020,

Thanks for your nice notes, I re-edited the post again, would you please to assist with this issue, thanks.

I have edited your post. For future posts, please ensure it is formatted thus:

```
This is some code...no backticks in the code. Only
wrapping the code.
```
1 Like