How to add spaces between the squares on a squarify plot - python

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.

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

plt.bar or ax.bar can not change color when I set the certain parameter [duplicate]

I hope you are well.
I am plotting a histogram using Matplotlib. I would like the color of the histogram to be "sky blue". But the data overlaps, and produces a histogram which is nearly black in color.
Thanks for helping
plt.hist(data, color = "skyblue")
Below is how the histogram looks. As you can see, though I've specified the color as "Skyblue, the histogram on the right is nearly black
The reason for the histogram to look black is that the bars' surrounding lines (which are black) take most of the space.
Options would be to get rid of the edges by setting the linewidth to zero:
plt.hist(data, color = "skyblue", lw=0)
and/or to set the edgecolor to the same color as the bars itself
plt.hist(data, color = "skyblue", ec="skyblue")

Is it possible to automatically scale the figure size, but keep the plot size constant in matplotlib?

I am using matplotlib to create multiple bar plots using the following code:
fig = plt.figure(figsize=(4, 4))
plt.barh(y=y, width=width, height=0.5)
plt.yticks(y, labels)
plt.xlabel("Contribution")
plt.tight_layout()
plt.show()
Since the length of my y-ticks labels can vary, the plot can get squeezed together as in the case below:
In other cases the plot looks fine:
Now, I was wondering, if there is an option in matplotlib to keep the plot size constant, but scale the figure size automatically (in horizontal direction)? My goal is that the plot size stays always the same, independent of the y-label length (because they vary inbetween plots). Thank you!

How to get N easily distinguishable colors with Matplotlib

I need to make different amounts of line plots with Matplotlib, but I have not been able to find a colormap that makes it easy to distinguish between the line plots.
I have used the brg colormap like this:
colors=brg(np.linspace(0,1,num_plots))
with
for i in range(num_plots):
ax.step(x,y,c=colors[i])
With four plots, this could look like this:
Notice how hard it is to distinguish the colors of the top and bottom plots, which is especially bad if a legend is used.
I've tried a lot of different colormaps like rainbow and jet, but with this setup, brg seems to give the best result for num_plots between 1 and 12.
I did find this How to get 10 different colors that are easily recognizable and this Wiki page Help:Distinguishable colors, but I don't know if this can be used in any way..
Is there an easy fix to this, or will I have to make do with this?
I would use the tab10 or tab20 colormaps. See Colormap reference
However, I believe you will always have trouble distinguishing hues when the number of lines becomes large (I would say >5 and certainly >10).
In this case, you should combine hues with other distinguishing features like different markers or linestyles.
colors = matplotlib.cm.tab20(range(20))
markers = matplotlib.lines.Line2D.markers.keys()
x = np.linspace(0,1,100)
fig, axs = plt.subplots(2,4, figsize=(4*4,4*2))
for nlines,ax0 in zip(np.arange(5,21,5), axs.T):
ax0[0].set_title('{:d} lines'.format(nlines))
for n,c,m in zip(range(nlines),colors,markers):
y = x*np.random.random()+np.random.random()
ax0[0].plot(x,y)
ax0[1].plot(x,y, marker=m, markevery=10)
axs[0,0].set_ylabel('only hues', fontsize=16, fontweight='bold')
axs[1,0].set_ylabel('hues+markers', fontsize=16, fontweight='bold')
fig.tight_layout()

matplotlib hatched and filled histograms

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).

Categories

Resources