How to save two different matplotlib figures to two lists - python
data = pd.read_csv("Test data.csv")
test_data = np.array(data)
where the test_data is a time series dataset which dimension is 24×1024. Firstly, I use a for loop to draw all raw data and use map method to save raw figures in figs_list. the map method reference is here:
Get the list of figures in matplotlib
def raw_data_draw():
for i in range(len(test_data)):
draw_data = test_data[i]
plt.figure(figsize=(3.5,2.4),dpi=100)
plt.xlim([0,1024])
plt.ylim([0,1])
plt.plot(draw_data)
plt.title("signal %d"%i)
global figs_list
figs_list = list(map(plt.figure, plt.get_fignums()))
return figs_list
secondly, I reshape the (24×1024) to (24×32×32) and draw grey scale images.
def grey_data_draw():
for i in range(len(test_data)):
grey_data = test_data.reshape(len(test_data),32,-1)
plt.figure(figsize=(2.5,2.5),dpi=100)
plt.imshow(grey_data[i],origin='lower', cmap=plt.cm.gray)
plt.title("signal %d"%i)
global grey_figs_list
grey_figs_list = list(map(plt.figure, plt.get_fignums()))
return grey_figs_list
Lastly, I run these two functions as follow and get two lists.
raw_data_draw()
grey_data_draw()
the figs_list should contain 24 time series plot and grey_figs_list should contain 24 grey images.
I expect that figs_list and grey_figs_list contain different items, anyhow these tow lists contains same items,which seems figure were overwrited. I have searched much material but can not deal with it. Hope you can help me solve this problem, thanks in advance! :)
the result is show as follow, where two lists contains same items.
Related
Python Array Copying
I'm trying to figure out how to copy an array output into a new array for multiple iterations. The scenario is to run a function in a for loop with varying inputs then overlay the results on a single plot for comparison. Currently I have it running where I get three arrays from the for loop, but this results in three independent plots. My coding is not very solid so some guidance would be appreciated. I was reading up on the list copy function but have not been able to get it to do what I want. for z,wn in mylist: G1 = y_numeric(z,wn) #np.array(output[i,:])=G1.copy() #plt.figure() #plt.plot(t,G1[:]) #print(G1) #print(output)
#user158430, Here is a simple example of a working code that might help you navigate through your code: import matplotlib.pyplot as plt import numpy as np #creating empty list to append all y variables all_y = [] #creating random variables x,y1,y2,y3=np.arange(0,50,1),np.arange(50,100,1),np.arange(100,150,1),np.arange(150,200,1) #appending only y variables into the created empty list all_y.extend((y1,y2,y3)) #looping to plot on one single figure for i in (all_y): plt.plot(x,i) plt.figure() #this code is kept outside the for loop if desired to print all the plots in one figure, if wanted the plots to be separated then indent it to match the plt.plot (i.e., put it in the loop) This is what you should get:
Adding multiple images to a matplotlib subplot?
I am trying to make a matplottlib plot using some image data I have in numpy format, and was wondering if someone would be able to advise me on the best way to approach displaying multiples of these images within the boundaries of one subplot? For example, using the following code... n_samples = 10 sample_imgs, min_index = visualise_n_way(n_samples) print(min_index) print(sample_imgs.shape) print(sample_imgs[0].shape) print(x_train_w) print(x_train_h) img_matrix = [] for index in range(1, len(sample_imgs)): img_matrix.append(np.reshape(sample_imgs[index], (x_train_w, x_train_h))) img_matrix = np.asarray(img_matrix) img_matrix = np.vstack(img_matrix) f, ax = plt.subplots(1, 3, figsize = (10, 12)) f.tight_layout() ax[0].imshow(np.reshape(sample_imgs[0], (x_train_w, x_train_h)),vmin=0, vmax=1,cmap='Greys') ax[0].set_title("Test Image") ax[1].imshow(img_matrix ,vmin=0, vmax=1,cmap='Greys') ax[1].set_title("Support Set") ax[2].imshow(np.reshape(sample_imgs[min_index], (x_train_w, x_train_h)),vmin=0, vmax=1,cmap='Greys') ax[2].set_title("Image most similar to Test Image in Support Set") I get the following image and output 1 (11, 784) (784,) 28 28 Matplotlib Output What I would like to do however is to have the second subplot, the one displaying img_matrix, to be the same size as the two either side of it, creating a grid of the images. Sort of like this sketch. I am at a loss as to how to do this however. I believe I may need to use something such as a gridspace, but I'm finding the documentation hard to follow for what I want to do. Any help is greatly appreciated!
Visualize multiple 2d Array with same color scheme
I am currently trying to visualize three 2D arrays with the same color. The arrays are 13x13 and contain integers. In an external file I have a color code in hex for each integer. When I now try to visualize the arrays, two out of three arrays look good. All numbers match the color codes and display the arrays correctly. But in the last picture a part of the data is not assigned correctly. . color_names = [c.strip() for c in open(colors).readlines()] color_dict = {v: k for v, k in enumerate(color_names)} unique_classes = (np.unique(np.asarray(feature_map))).tolist() number_classes = len(unique_classes) color_code = [color_dict.get(cla) for cla in unique_classes] cmap = plt.colors.ListedColormap(color_code) norm = plt.colors.BoundaryNorm(unique_classes, cmap.N) img = pyplot.imshow(feature_map[0],interpolation='nearest', cmap = cmap,norm=norm) pyplot.colorbar(img,cmap=cmap, norm=norm,boundaries=unique_classes) pyplot.show() img1 = pyplot.imshow(feature_map[1],interpolation='nearest', cmap = cmap,norm=norm) pyplot.show() img2 = pyplot.imshow(feature_map[2],interpolation='nearest', cmap = cmap,norm=norm) pyplot.colorbar(img2,cmap=cmap, norm=norm,boundaries=unique_classes) pyplot.show() Exactly the same data as on the picture: feature_map = [[[25,25,25,25,56,56,2,2,2,2,2,2,25],[25,25,25,25,25,25,59,7,72,72,72,72,2],[25,25,25,25,25,25,59,72,72,72,72,72,2],[25,25,25,24,24,24,62,0,0,0,0,25,25],[25,25,24,24,24,24,24,24,24,24,25,25,25],[26,26,24,24,24,24,24,26,26,26,6,6,6],[26,26,26,24,24,26,26,26,26,26,26,6,6],[26,26,26,0,0,26,26,26,26,26,26,6,6],[28,28,28,28,28,28,28,26,26,26,26,6,6],[28,28,28,28,28,28,28,26,26,26,13,13,6],[28,28,28,28,28,28,28,26,13,13,13,13,13],[28,28,28,28,28,28,28,13,13,13,13,13,13],[28,28,28,28,28,28,28,13,13,13,13,13,13]],[[25,25,25,25,59,56,59,2,0,0,0,0,0],[25,25,25,25,25,59,59,7,72,72,72,72,72],[25,25,25,25,25,25,59,72,72,72,72,72,72],[25,25,25,0,0,25,25,6,0,0,0,72,0],[25,25,0,0,0,0,6,0,0,0,0,25,6],[26,26,26,0,0,0,24,26,0,0,6,6,6],[26,26,26,0,0,0,26,26,26,26,26,6,6],[0,26,0,0,0,0,26,26,0,26,26,6,6],[0,28,28,28,28,28,28,26,0,26,26,6,6],[28,28,28,28,28,28,28,26,0,26,0,0,0],[28,28,28,28,28,28,28,26,13,13,13,13,0],[56,56,28,28,28,28,28,13,13,13,13,13,13]],[[0,28,28,28,28,28,28,13,13,13,13,13,0],[25,25,25,25,59,59,59,4,0,0,0,0,0],[25,25,25,25,59,59,59,7,7,7,72,72,6],[25,25,25,25,25,25,59,7,7,73,73,25,0],[25,25,25,0,0,25,6,7,0,6,6,6,0],[25,0,0,0,6,6,6,6,0,0,6,6,6],[0,0,0,0,0,6,6,6,0,0,6,6,6],[0,0,0,0,0,0,6,6,0,0,6,6,6],[0,0,0,0,0,0,6,0,0,0,6,6,6],[0,0,28,0,28,28,13,0,0,0,6,6,6],[28,28,28,28,28,28,13,13,13,0,13,6,6],[28,28,28,28,28,28,28,13,13,13,13,13,13],[56,28,28,28,28,28,28,13,13,13,13,13,13],[28,28,28,28,28,28,28,13,13,13,13,13,13]]] The color code file is simply a file where each line contains a single hex code such as: #deb887 I have been working on this problem for several hours and can't reproduce the problem at the moment
I have tried to reproduce your results and something got my attention. If you look closely to the feature_map[2] values you might see that the pixel you claim miss classified has actually a different value than the pixels around it. So it actually has the correct color for its value. So I think it is not because of a misclassification it is beacause of your data. That would be my answer IF what you mean by "part of the data" is the pixel at position (0,11) otherwise i have gotten it all wrong and sorry about this answer. NOTE: About colors, I just picked some random colors. Don't worry if they don't match.
remove overlay text from pandas boxplot
I am trying to remove the overlay text on my boxplot I created using pandas. The code to generate it is as follows (minus a few other modifications): ax = df.boxplot(column='min2',by=df['geomfull'],ax=axes,grid=False,vert=False, sym='',return_type='dict') I just want to remove the "boxplot grouped by 0..." etc. and I can't work out what object it is in the plot. I thought it was an overflowing title but I can't find where the text is coming from! Thanks in advance. EDIT: I found a work around which is to construct a new pandas frame with just the relevant list of things I want to box (removing all other variables). data = {} maps = ['BA4','BA5','BB4','CA4','CA5','EA4','EA5','EB4','EC4','EX4','EX5'] for mapi in maps: mask = (df['geomfull'] == mapi) arr = np.array(df['min2'][mask]) data[mapi] = arr dfsub = pd.DataFrame(data) Then I can use the df.plot routines as per examples.... bp = dfsub.plot(kind='box',ax=ax, vert=False,return_type='dict',sym='',grid=False) This produces the same plot without the overlay.
Create a color generator from given colormap in matplotlib
I have a series of lines that each need to be plotted with a separate colour. Each line is actually made up of several data sets (positive, negative regions etc.) and so I'd like to be able to create a generator that will feed one colour at a time across a spectrum, for example the gist_rainbow map shown here. I have found the following works but it seems very complicated and more importantly difficult to remember, from pylab import * NUM_COLORS = 22 mp = cm.datad['gist_rainbow'] get_color = matplotlib.colors.LinearSegmentedColormap.from_list(mp, colors=['r', 'b'], N=NUM_COLORS) ... # Then in a for loop this_color = get_color(float(i)/NUM_COLORS) Moreover, it does not cover the range of colours in the gist_rainbow map, I have to redefine a map. Maybe a generator is not the best way to do this, if so what is the accepted way?
To index colors from a specific colormap you can use: import pylab NUM_COLORS = 22 cm = pylab.get_cmap('gist_rainbow') for i in range(NUM_COLORS): color = cm(1.*i/NUM_COLORS) # color will now be an RGBA tuple # or if you really want a generator: cgen = (cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS))