I'm trying to show 2 subplots with x-axis labels rotated 90 degrees. The first subplot's labels for some reason are not rotating. Can anyone let me know what I'm missing? Thanks!
fig, ax = plt.subplots(1, 2, sharey=True, figsize=(20,10))
fig.suptitle('Top and Bottom Countries by Rating')
plt.xticks(rotation=90)
sns.barplot(data=top_df, y='rating', x='bean_origin', ax=ax[0], palette="Blues_d")
ax[0].set_title('Top 10 Countries by Avg Rating')
sns.barplot(data=bottom_df, y='rating', x='bean_origin', ax=ax[1], palette="Reds_d")
ax[1].set_title('Bottom 10 Countries by Avg Rating')
plt.show()
Link to chart image
Try this:
1. Call this AFTER plotting: if 1 time not enough, try to add it after each plotting
sns.barplot('BLA BLA')
plt.xticks(rotation=90)
Otherwise try one of these solutions below:
2. :
fig.autofmt_xdate(rotation=90)
3.
labels = ['One', 'Two', 'Three'] #your labels
ax.set_xticks([1, 2, 3]) #your coord
ax.set_xticklabels(labels, rotation=90)
4.
ax.tick_params(axis='x', labelrotation=90)
If none above works, provide full code.
Note: IMO, 45° is better :)
Related
I am trying to flip over the colorbar of my Heatmap in Seaborn.
Here is how it looks at the moment.
What I would like to have is the colorbar starting from the top
with the value 0 (Green) and going to the bottom with the value 8 (red).
Please note that the Y-axis points are sorted based on the average values
from min (top) to max (bottom) and I would like to keep them this way.
Any idea if it is possible to do that?
Here is an example of the current code:
cmap1 = mcolors.LinearSegmentedColormap.from_list("n",['#00FF00','#12FF00','#24FF00','#35FF00','#47FF00','#58FF00','#6AFF00','#7CFF00','#8DFF00','#9FFF00','#B0FF00','#C2FF00','#D4FF00','#E5FF00','#F7FF00','#FFF600','#FFE400','#FFD300','#FFC100','#FFAF00','#FF9E00','#FF8C00','#FF7B00','#FF6900','#FF5700','#FF4600','#FF3400','#FF2300','#FF1100','#FF0000',])
plt.figure(figsize=(22, 12))
df = pd.DataFrame( AgainReorderindSortedEDPList, index=sortedProgrammingLanguagesBasedOnAverage, columns=sortedTasksBasedOnAverage)
mask = df.isnull()
sns.heatmap(df, annot=True, fmt="g", cmap=cmap1, mask=mask)
plt.yticks(fontsize = 12)
plt.yticks(rotation=0)
plt.xticks(fontsize = 11)
plt.ylabel('Programming Languages', size = 15)
plt.xlabel('Programming Tasks', size = 15)
plt.xticks(rotation=-45)
plt.show()
The AgainReorderindSortedEDPList, sortedProgrammingLanguagesBasedOnAverage, and sortedTasksBasedOnAverage
are the data I am using to plot this heatmap.
You simply need to call invert_yaxis() on the axes that contain the colorbar. How to do that depends a bit on how you are creating your heatmap, but unfortunately you have not provided your code.
Here is the most simple example:
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
plt.gcf().axes[1].invert_yaxis()
plt.gcf() gets a reference to the current figure. Figure.axes is a list of axes in the figure. axes[1] is the second axes, which should correspond to the axes created by heatmap to plot the colorbar.
I have an image of 200x200 * 3, and I should to find the x and the y of each pixel of the image by using the linspace Matlab function.
I have already used the mesh grid function with the following code:
Ny=200; % # pixels in y direction
Nx=200; % # pixels in x direction
resolution=1; % size of a pixel
img=rand(Ny,Nx,3); % random image
y=(img(1:Ny))*resolution-resolution/2;
x=(img(1:Nx))*resolution-resolution/2;
[x,y]=meshgrid(x,y);
But my supervisor told me to use the function linspace and I cannot understand how.Can someone help me please
The only reason I can think of that your supervisor wants you to use linspace is for creating the x and y vector inputs to meshgrid. linspace alone is not sufficient to generate all of the pixel coordinates, so you'll have to use meshgrid, ndgrid, repelem or repmat.
The one advantage that linspace has over simply doing something like 1:resolution:Ny is that linspace ensures that the endpoints always appear in the range. For example, if you do
>> 1:.37:4
ans =
1.0000 1.3700 1.7400 2.1100 2.4800 2.8500 3.2200 3.5900 3.9600
you don't get 4 as the last point. However, if you use linspace:
>> linspace(1,4,9)
ans =
1.0000 1.3750 1.7500 2.1250 2.5000 2.8750 3.2500 3.6250 4.0000
the spacing is automatically adjusted to make the last element 4.
So assuming that resolution is the multiplier for the number of points you want, for 2 you want 401 evenly spaced points and for 1/2 you want 101 points, your code would look something like this:
x_points = linspace(1, Nx, Nx*resolution+1);
y_points = linspace(1, Ny, Ny*resolution+1);
(I'm still not sure what the resolution/2 part is supposed to do, so we'll need to clarify that.)
Then you would pass those to meshgrid:
[X,Y] = meshgrid(x_points, y_points);
I have a plot I get from matlab, with the x-axis ranging from 0 to 1864 values. I want to save this plot as a .png image without need to modify the original .fig file manually
My plot looks like this:
As you can see, the last two numbers are overlapping. The reason is that when matlab displays my image, the popup window is too small. I already tried to change the axes ratio using daspect, but it does not work.
What I think could work is to use the function truesize, my problem is that when I try to use it, I receive the following error:
No images or texturemapped surfaces in the figure.
My code looks like this:
x = rand (1864,1);
F = bar (x);
xlim ([0 1864]);
set(gca, 'XTick', sort([1864, get(gca, 'XTick')]));
truesize(1,[100 100])
Why does this happen? How can I fix this problem in order to save the images preventing x labels overlapping?
You could try rotating your x labels using the following code:
x = rand (1864,1);
F = bar (x);
xlim ([0 1864]);
set(gca, 'XTick', sort([1864, get(gca, 'XTick')]));
set(gca, 'XTickLabelRotation', 90)
Note that you can change the degrees to rotate to other number besides 90 if you wanted.
It gives you a plot that looks like this:
I guess this is a very basic question.
I have a graph which I created in MATLAB. This is a graph of Power (y-axis) versus Frequency (x-axis).
The range of my x-axis is from 0 to 1000. Now here is my problem. I want to draw a line from specific points on the x-axis to the graph. For example, for points 40, 400, 950.
By using set(gca, 'XTick', [40 400 950]); I am able to mark these particular points. But I want to make it more visible by drawing straight vertical lines from these points.
Any help will be greatly appreciated. Thank you.
Use plot with endpoints with the same x value and different y values. (and don't forget to use myaa to beautify the output).
x = 0:0.1:2*pi;
y = sin(x);
plot(x,y);
hold on;
plot([0.6 0.6], [-1 1], 'Color', [0.7 0.7 0.7], 'LineWidth', 2);
plot([3.6 3.6], [-1 1], 'Color', [0.7 0.7 0.7], 'LineWidth', 2);
If you do this often I would recommend you a great submission from the FileExchange:
hline and vline
Just do:
vline([40 400 950])
Read the function documentation, if you want the line to have different properties than default.
I typically do this using something like this (is powers is a row vector).
powers = randn(1,1000)+40;
plot([1;1]*[40 400 950], [[0 0 0]; [powers([40 400 950])]],'k-')
I have the following image on which i have applied bilateral filtering and subtracted it from my original image
Is it possible to outline the glass defect as shown
After apply Hough i got the following result not perfect :/
My matlab code :
im = imread('C:\Users\SUJIT\Desktop\image003.jpg');
im=rgb2gray(im);
h = fspecial('gaussian', size(im), 1.0);
g = imfilter(im, h);
im=im2double(g);
im=imadjust(im);
imgauss = imfilter(im, fspecial('gaussian',[7 7], 6),'conv');
imbi = bilateralfilter(im, [5 5], 3, 3);
imbi= im - imbi;
imshow(imbi,[]); title('Bilateral Filted Image');
I = imcrop(imbi, [30 30 450 350]);
J = imfilter(I, fspecial('gaussian', [17 17], 5), 'symmetric');
BW = edge(J, 'canny');
%# Hough Transform and show matrix
[H T R] = hough(BW);
imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, ...
'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho')
axis on, axis normal, hold on
colormap(hot), colorbar
%# detect peaks
P = houghpeaks(H, 10);
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2);
%# detect lines and overlay on top of image
lines = houghlines(BW, T, R, P);
figure, imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end
hold off
Please help am i doing something wrong here?
It is a bit hard to give a general answer based on only one image and no other information, but I can give a specific answer based on your sample image.
Assuming that what you want to find is the vertical blurry line in the middle of the image, this my approach. I won't go into specific implementation details, but only an outline on how I would do it.
Find the windows. There are multiple approaches to this. Some ideas are to either find the corners, or to find the rectangular structure itself. The Hough transform is a possible tool.
For each window, check if there are vertical structures in it.