matplotlib hatched and filled histograms - python

I would like to make histograms that are both hatched and filled (like these bar plots on the left in this matplotlib example):
Here's the code I tried to use:
import matplotlib.pyplot as plt
plt.hist(values, bins, histtype='step', linewidth=2, facecolor='c', hatch='/')
But no matter whether I specify "facecolor" or "color", only the lines of the hatching appear in colour and the histogram is still unfilled. How can I make the hatching show up on top of a filled histogram?

In order to fill the area below the histogram the kwarg fill can be set to True. Then, the facecolor and edgecolor can be set in order to use different colors for the hatch and the background.
plt.hist(np.random.normal(size=500), bins=10, histtype='step', linewidth=2, facecolor='c',
hatch='/', edgecolor='k',fill=True)
This generates the following output:

histtype='step'draws step lines. They are by definition not filled (because they are lines.
Instead, use histtype='bar' (which is the default, so you may equally leave it out completely).

Related

How can I use alpha with seaborn.pointplot? [duplicate]

I want to make a seaborn pointplot that has transparency so that I can clearly see the points located behind others of a different color.
I tried adding "alpha=0.3" to the call to pointplot and also tried the same within a catplot with kind='point'; however, neither of these results in the desired transparency (no error message is produced either).
sns.pointplot(x='aamm', y='posrate', hue='AA:XX', hue_order=[1,0], data=data, dodge=True, palette=palette, alpha=0.3)
I was hoping to get a plot with transparent points, but instead, I got one with normal opaque points. The dodge option doesn't seem to produce any noticeable effect either, in terms of separating overlapping points of different color.
Is there a way to add transparency to a seaborn pointplot or use something else to get a similar effect?
Thank you.
To the extent of my knowledge there is no more an alpha parameter that can be directly set in seaborn.
You can do the following thou:
Sample dataframe
df = pd.DataFrame(np.random.randint(low=0, high=1000, size=(50, 5)))
Plotting
ax = sns.pointplot(x=0, y=1, data=df, dodge=True,plot_kws=dict(alpha=0.3))
plt.setp(ax.collections, alpha=.3) #for the markers
plt.setp(ax.lines, alpha=.3) #for the lines

How to customize plots that were made using "scikitplot.metrics" other than the arguments show in the function?

I made some graphs with scikit-plot package, but need to customize the axes colors and sizes as well the lines width.
Here is what I already have plotted and the editable arguments of the function generating the plot:
skplt.metrics.plot_ks_statistic(y_train,
random_forest.predict_proba(x_train[["out_prncp",
"int_rate","total_rec_prnc","total_pymnt"]]),ax=None, figsize=(15,7), title_fontsize="large",
text_fontsize="medium")
plt.xlim([0,0.7])
plt.title("KS Modelo Random Forest - Treino", size=20)
plt.rc("lines", linewidth=1.5, color='r')
plt.autoscale()
As show, the param arguments of matplotlib.pyplot to format are disregarded for some reason.
Just need to set the graph lines to 1.5 points of widths and black colors in the axis ticks
Quick excel hack to show the formating:

Combine two matplotlib markers into one in legend

To have a custom marker, I made two scatter plots with same data points but different markers. Thus by plotting one marker on top of the other I get the look of a new custom marker. Now I want to use it in legend. Is there a way I can use two markers one on top of the other in legend and show them as a single marker.
Edit:
The question is not regarding how to share the same label for two different markers, but how to plot one marker on top of other in the legend
Using a tuple of markers answers the question;
from numpy.random import randn
m=np.random.uniform(size=10)
x=np.arange(0,10,1)
y=x**2
fig, ax = plt.subplots(1,1)
blue_dot = ax.scatter(x[:5],y[:5], s=m*100, color='b')
red_dot = ax.scatter(x[5:],y[5:], s=200*m, color='r')
black_cross = ax.scatter(x[5:],y[5:], s=400*m, marker='+', color='k')
lgnd = ax.legend([blue_dot, (red_dot, black_cross)], ["Blue Circle", "Red Circle and Black Cross"])
Now I want to change the size of the markers in the legend so that all the markers of equal size. For that, I have tried adding this to above code.
lgnd.legendHandles[0]._sizes = [200]
lgnd.legendHandles[1]._sizes = [200] # this is affecting the size of red_dot only
How do I change the size of black_cross as well in the legend?

How to add spaces between the squares on a squarify plot

I want to change the colors of the following graph to make it more specific to my case, but then I won't be able to see the different sizes (many squares will be the same).
My program actually is :
squarify.plot(sizes=sizeTab, alpha=.8 )
plt.axis('off')
plt.title('Plottitle')
plt.show()
is there a simple way to add a small spacing between the squares ?
The squarify graph produces bars. It would be hard to change the bar coordinates to allow for spacings, but giving the bars an edgecolor of the same color as the background (white) would have the same visual effect. So an option is to use
squarify.plot(sizes=sizeTab, alpha=.8, edgecolor="white", linewidth=2)
Change the linewidth to get more or less spacing.

Matplotlib: how to adjust space between legend markers and labels?

I want to adjust space between legend markers and labels. Sometime the space is too much as default. Does anyone know how to do this?
Thanks.
legend() has a kwarg in called handletextpad which will do what you are looking for. By default, this is set to 0.8. From the docs:
handletextpad : float or None
The pad between the legend handle and text. Measured in font-size
units.
Default is None which will take the value from the
legend.handletextpad rcParam.
So when you call legend, add that kwarg, and experiment with the value. Something like:
ax.legend(handletextpad=0.1)
Consider the following:
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.plot(range(5), 'ro', label='handletextpad=0.8')
ax2.plot(range(5), 'bo', label='handletextpad=0.1')
ax1.legend()
ax2.legend(handletextpad=0.1)
plt.show()

Categories

Resources