This question already has answers here:
How to adjust padding with cutoff or overlapping labels
(8 answers)
matplotlib savefig - text chopped off
(3 answers)
Closed 4 months ago.
I have the following code to create a heatmap with seaborn! unfourtunately my y-ticklabels are long names, and are cut off from the figure as shown in the attached image!
seasons = ['yearly','djf','mam','jja','son']
for season_index,season in enumerate(['yearly','djf','mam','jja','son']):
im = sns.heatmap(bias[:,season_index,:],linewidth = 0.5,cmap='coolwarm', annot=True,annot_kws={'fontsize':7, 'fontweight':'bold'})
im.set_xticklabels(['1','2','3','4','5','6','7','8','9'])
im.set_yticklabels(model_list)
plt.savefig(f"figures/bias/yearly_bias_{seasons[season_index]}.png")
plt.close()
I tried changing the figure size with matplotlib, but everytime i did that, the tick labels would rotate 90° and overlap, and in my code it didn't work to reset it with the rotation keyword in the set_yticklabels function.
i also tried this but that had the same result in turning the labels.
Thank you!
plt.tight_layout()
this worked, thank you ver ymuch to Nikita Shabankin!
Related
This question already has answers here:
matplotlib/seaborn: first and last row cut in half of heatmap plot
(10 answers)
Closed 3 years ago.
The confusion matrix is displaying data out of frame. I tried resizing the figure where the figure area is changing but the data is still out of frame. Any idea? Thanks in advance.
cm = confusion_matrix(decoded_y_test, predictions)
cm_df = pd.DataFrame(cm,
index = ['HIT','AVERAGE','FLOP'],
columns = ['HIT','AVERAGE','FLOP'])
plt.figure(figsize=(5.5,4))
sns.heatmap(cm_df, annot=True)
This seems similar to this matplotlib/seaborn: first and last row cut in half of heatmap plot
What version of matplotlib are you using? Reverting to version 3.1.0 seemed to fix this issue.
This question already has answers here:
No outlines on bins of Matplotlib histograms or Seaborn distplots
(3 answers)
Closed 5 years ago.
df3['a'].plot.hist(color='blue',xlim=(0,1))
I want to know how can it show the line in the histogram figure.
Make the top figure showed as bottom figure. Thank you!
Pass the edgecolor argument to hist.
df3['a'].plot.hist(color='blue',
edgecolor='black',
xlim=(0,1))
Demo
df = pd.DataFrame(dict(vals=np.random.normal(size=100)))
df.plot.hist(edgecolor='black')
This question already has answers here:
How can I change the x axis in matplotlib so there is no white space?
(2 answers)
Closed 5 years ago.
I currently have a graph that looks like this using mathplotlib
However the margins between where the line actually starts and the edges of the graph are unnecessary, do you know how I can get rid of the margins so 0,0 starts at the corner?
For example, I want it to look like this
plt.xlim([0, x_max])
plt.ylim([0, y_max])
You can easily get the values of (x_max, y_max) from your data.
This question already has answers here:
How to remove relative shift in matplotlib axis
(2 answers)
Closed 8 years ago.
I am using Python and Pyplot to produce a plot. The y axis of the plot is generated automatically, and works fine.
If the range is small however, the y axis will produce values such as:
0.00005,
0.00010,
0.00015,
0.00020,
0.00025,
0.00030,
and then at the top of the axis, will say:
+1.543e-1
I would prefer that it just explicitly shows the values:
0.15435
0.15440
0.15445
0.15450
0.15455
0.15460
Could someone tell me how to do this please?
Alright, I think I found the total solution:
Here is a quick plot that recreates your problem:
plot((0.15435,0.15440,0.15445,0.15450,0.15455,0.15460),(0.15435,0.15440,0.15445,0.15450,0.15455,0.15460))
The following code (similar to how it was as shown here) should adjust the ticker like you want:
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
gca().yaxis.set_major_formatter(y_formatter)
This question already has answers here:
Heatmap in matplotlib with pcolor?
(4 answers)
Closed 9 years ago.
This is a very basic question. I could not find a satisfactory answer anywhere else so I am writing this up as a question here. I have a matrix, a square matrix about 1300 x 1300. I can use matplotlib to generate a heatmap from it. However, I want the row and column names to show up on the heat map instead of the 0 -- 1300 that normally shows up when i use imshow.
I will put up an example shortly.
You still have not put up your example, but I will give you a quick example of how to change the labels on each axis!
First, put your labels in an array, let's call them y_labels and x_labels
Now here is your code:
ax = pylab.subplots()
ax.set_yticklabels(y_labels)
ax.set_xticklabels(x_labels)
That should do the trick!