I have a raster data which contains 40 band total. Here is the head of my data:
So I created a code that can read and calculate the mean of the bands like this:
prof = train_pts.groupby (['classes']).mean ()
fig = plt.figure(figsize = (17,20))
band_n = [ 'Band [2,6,11,16]', 'Band [3,7,12,17]', 'Band [4,8,13,30]', 'Band [20,22,14,26]', 'Band [2,26,11,30]', 'Band [2,26,11,30]']
band_name = ['Sentinel-2 B2'], ['Sentinel-2 B3']
n = 1
for ba in band_n :
ax = fig.add_subplot(4,2,n)
ax.title.set_text(band_name)
band_val = prof[prof.columns[prof.columns.to_series().str.contains(ba)]]
for index, row in band_val.iterrows():
color = cmap (index)
ax.plot (row,color=color)
ax.autoscale(enable=True, axis="both", tight=None)
ax.set_xticklabels([(str (band_name) for band_name in range(1, len(row) 1))])
ax.legend(loc='best', fontsize='small', ncol=2, labels=class_names)
n=n 1
For example, for Sentinel-2 B2 it calculates the mean of these bands for each point and plot them. But I am struggling with x, y axis labels and the title of the plots. What I wanted to do here is set the 'Band [2,6,11,16]'
plot's label as 'Sentinel-2 B2'
, 'Band [3,7,12,17]'
plot's label as ['Sentinel-2 B3']
and go on like this... But it write the same title for all plots that I created. Like this:
What can I try else?
CodePudding user response:
In your code, band name never changes:
band_name = ['Sentinel-2 B2'], ['Sentinel-2 B3']
n = 1
for ba in band_n :
ax = fig.add_subplot(4,2,n)
ax.title.set_text(band_name) # this is unaffected by the loop
I think you mean something along these lines... making use of the enumerate built in function
band_name = ['Sentinel-2 B2', 'Sentinel-2 B3']
for i, ba in enumerate(band_n):
ax = fig.add_subplot(4,2,i 1)
ax.title.set_text(band_name[i])
But you don't have enough band_names for the number of bands, so you'll need to fix your band_name
list before this code will work.