When I add in the formatting of the x axis, the graph remains the same. When I format the y axis tick labels, the graph reverses and I can't figure out why.
Heatmap before y-axis format
Here is the graph after the x-axis format
ax = sns.heatmap(All_new, linewidths=.5, annot=True, cmap="RdYlGn")
plt.tight_layout()
plt.xlabel('Losing Digit')
plt.ylabel('Winning Digit')
plt.title('Total Distribution of Final Digits (%)')
ax.get_xaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x))))
ax.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x))))
#ax.invert_yaxis()
plt.show()
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.
Is there a way to limit the number of decimal places in the legend of a seaborn scatter plot?
here is the code I have written so far. Cannot find a way to format the number in the legend (set by the variable z)
fig, ax0 = plt.subplots()
sns.scatterplot(x='x',
y='y',
data=df,
size='z',
sizes=(50, 300),
size_norm=(0,0.5),
palette=custom_palette,
ax=ax0
)
ax0.set(xscale='log')
plt.show()
I have a 1265x1777 matrix with the intensity values of an image. I need to develop a point cloud file in MATLAB for the same. Just like a 3D scatter where x, y and z coordinates are stored in a variable; x, y should represent the pixel location; and z corresponds to the intensity of that pixel.
Edit: Updated according to OP's comment.
Assuming your 1265x1777 matrix is called 'img':
x = 1:size(img,2);
y = 1:size(img,1);
[X,Y] = meshgrid(x,y);
xyz_matrix = [X(:), Y(:), img(:)];
I'm trying to plot some data in seaborn where the x values are percentages*100 as floating point numbers (ie 90.909). When I make the plot:
fig, ax = plt.subplots(figsize=(10,10))
ax = sns.stripplot(df_12['% ident'], df_12['length'], jitter=True)
The decimals in the floating points make the X axis unreadable:
Initial Plot
I would like to set the x axis to show only whole number multiples of 5 (ie 80, 85, 90, 95, 100).
One method I have tried is the following:
fmt = '{:0.0f}'
xticklabels = []
count = 0
for item in ax.get_xticklabels():
count+= 1
item.set_text(fmt.format(float(item.get_text())));
xticklabels += [item];
ax.set_xticklabels(xticklabels);
This succeeds in changing the axis values to integers, but the axis looks busy. The numbers shown are also inconsistent between similar datasets.
Second Plot
I would like to reduce the total number of values shown on the axis. I have tried to use
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
Or similarly
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
ax.set_xticklabels([80, 85, 90, 95, 100])
Which give outputs similar to this:
Third Plot
If you compare this to the previous plot, you'll notice the x axis labels no longer relate to the points plotted. How do I set the values of the x axis while still keeping them related to the points plotted?
Other things I have tried:
ax.set_xlim(75, 100)
This and any variants result in a blank plot.
ax.set(xticklabels=[75,80,85,90,95,100])
Does the same thing where the axis labels don't match the data.
ax.set(xticks=range(75,101), xticklabels=[75,80,85,90,95,100])
Results in all the data points stuck on the left side of the plot with all the axis labels overlapping on a single tick on the right.
ax.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
This doesn't change the axis values to integers, and also appears to cause the axis to no longer correlate with the data.
I have obtained the latitude, longitude and height information from a TIFF image into Matlab and plotting it using the surfc function as surfc(X,Y,Z).
surfc output:
Image of the tree:
But how do I get Z to display as a contour (something like a cone) corresponding to the height of an object in the image?
Thanks for any answers
You can change the color mapping of the surf. Example:
clear;clc;close all
tif4 = imread('RzwK3.tif');
THeight = rgb2gray(tif4(:,:,1:3));
imshow(THeight)
x = 1:size(THeight,1); % can be changed to coordinates
y = 1:size(THeight,2);
[X,Y] = ndgrid(x,y);
% make contour color on the surface
M = 4; % # color in contour
Cvec = parula(M); % any Mx3 RGB triplet
hs = surf(X,Y,THeight,'EdgeAlpha',.1);
colormap(Cvec)
colorbar
If parula or other color generation function is not available in your Matlab version, you can assign Cvec manually. Each row of the matrix is a RGB color triplet with values between 0 and 1 (you can divide a web RGB color by 256) and there should be M rows in the matrix. Example: the following is the output of parula(4), which can be manually input by replacing the line of code.
Cvec = [
0.2081 0.1663 0.5292; % R1 G1 B1
0.0265 0.6137 0.8135; % R2 G2 B2
0.6473 0.7456 0.4188; % R3 G3 B3
0.9763 0.9831 0.0538]; %R4 G4 B4
If you have the pionts as cartesian coordinates use plot3
plot3(X,Y,Z,'.');
If X and Y are the independent variables and Z the dependent one (The corresponding height for a given X and Y) then use surf or mesh
surf(X,Y,Z);
mesh(X,Y,Z);
Otherwise put some sample of your data and results so we can better understand your problem.
EDIT: You are actually obtaining a contour plot, but surfc should also plot the surface.
Take a look at the documentation of surfc and you will see that it combines two plots in one figure (the surface and the contour)
[X,Y,Z] = peaks(30);
figure
surfc(X,Y,Z)