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')
Related
This question already has answers here:
Matplotlib scatter plot legend
(5 answers)
Matplotlib - Adding legend to scatter plot [duplicate]
(1 answer)
Matplotlib scatter plot with legend
(6 answers)
Closed last month.
I'm trying to set the legend according to categorical values set for color but it's not working.
import matplotlib.pyplot as plt
colors = finalDf['sales'].astype('category').cat.codes
scatter = plt.scatter(x= finalDf['principal component 1'],
y= finalDf['principal component 2'],
c = finalDf['sales'].astype('category').cat.codes)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend(labels=list(finalDf['sales'].unique()))
plt.show()
(https://i.stack.imgur.com/YwSoG.png)
I tried various combinations of the 'sales' data columns but it was in vain. I want to know if there is a solution other than using seaborn library as the task recquires matplotlib. Thank you
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!
This question already has answers here:
How to change font properties of a matplotlib colorbar label?
(5 answers)
Closed 9 months ago.
Here's the code I have:
cbar = plt.colorbar(ScalarMappable(cmap=cm, norm=plt.Normalize(0, cycles - 1)), ticks=np.arange(cycles), label='cycles',location = 'bottom')
cbar.ax.tick_params(labelsize=20)
And the output looks like
I can make the ticks have the size I want to be, but is there a way I can also change the font size of 'cycles' here? Thanks for the help!
This two-liner can be used with any Text property (http://matplotlib.org/api/text_api.html#matplotlib.text.Text)
cb = plt.colorbar()
cb.set_label(label='a label',weight='bold')
or:
plt.colorbar().set_label(label='a label',size=15,weight='bold')
This question already has answers here:
Python Matplotlib Boxplot Color
(4 answers)
Closed 2 years ago.
I am struggling to plot a boxplot for custom column names of dataframe and afterwards fill them with custom different colors.
df=pd.DataFrame([[1,2,3],[3,4,3],[3,4,3]],columns=['x','y','z'])
df.boxplot(column=['x', 'y'])
plt.show()
I can't customize colours of my boxplots. Is there any way how to do it with simple code?
Try using this:
df = pd.DataFrame([[1,2,3],[3,4,3],[3,4,3]], columns=['x','y','z'])
box = plt.boxplot([df['x'], df['y']], patch_artist=True)
colors = ['blue', 'green']
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
plt.show()
This question already has answers here:
Plot a horizontal line on a given plot
(7 answers)
Closed 4 years ago.
This code (matplotlib.pyplot) gives me the plot in the link below:
plt.subplot(2, 1, 1)
plt.plot(px,py)
plt.subplot(2, 1, 2)
plt.plot(curve)
2 plots example
--> I want to add a horizontal line in the second sub-plot at 100.000. How can I do that? The colors of both plots shall stay the same/synchronized.
You can use matplotlib.axes.Axes.axhline of matplotlib which adds a horizontal line across the axis. If you need to set any further parameters, refer to the official documentation
import matplotlib.pyplot as plt
plt.axhline(100000, color="gray")