Seaborn chart colors are different than those specified by palette - matplotlib

Why are seaborn chart colors different from the colors specified by the palette?
The following two charts show the difference between the colors as they appear on a bar chart, and the colors as they appear in the palette plot. You can see if yo ulook carefully, that the colors on the bar chart are slightly less bright/saturated.
Why are these different, and how can I get the bar chart to have the exact same colors as the ones specified in the palette?
import seaborn as sns
sns.set(style="white")
titanic = sns.load_dataset("titanic")
colors = ["windows blue", "amber", "greyish", "faded green", "dusty
purple"]
ax = sns.countplot(x="class", data=titanic,
palette=sns.xkcd_palette(colors))
sns.palplot(sns.xkcd_palette(colors))
Bar chart
Palette plot

Many seaborn plotting commands have an argument saturation, whose default value is 0.75. It sets the saturation (S) of the colors in the HSL colorspace (ranging from 0 to 1) to the given value.
Setting this parameter to 1 in the countplot will give you the same colors in both plots.
ax = sns.countplot(x="class", data=titanic, palette=sns.xkcd_palette(colors), saturation=1)
sns.palplot(sns.xkcd_palette(colors))
The reason for this default desaturation is that many people consider a plot with less contrast to be more appealing. That is also why the default background in seaborn is not white but some bluish gray. After all, this is of course a question of taste.

Related

hvplot quadmesh custom dynamic cmap

I am trying to create a rangeSlider that controls the colorbar and bins of a hvplot quadmesh plot of gridded data. Right now I am using cmap and it is wonderful but I need a way to bin and color the data to a 3 color scheme namely,
(min, rangeSlider[0]) = Green labeled Good
(rangeSlider[0], rangeSlider[1]) = Yellow labeled Caution
(rangeSlider[1], max) = Red labeled Dangerous
So I made a couple of attempts but am not sure how to pass a ListedColormap from Matplotlib.colors as well as labels to a "bining" function of the quadmesh hvplot object.

Change in color of legend not reflected in the plot

I'm trying to change the colour of the '0's in my plot to red. I could change the colour of the legend but this isn't reflecting to that dots in the scatter plot. How do I fix this?
As the data is not available above, I am assuming that over_50['target'] has only two values - 0 or 1. In that case, you should be using palette to assign colors. legend_handles will update the colors and not the graph.
I am using a sample code to show this....
import seaborn as sns
tips = sns.load_dataset("tips")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", palette = ['r', 'g']) #Red and Green
Output

Control order of Tab10 colors in colormap

I would like to get a colormap with the first two colors of the Tab10 palette (blue and orange). But
import matplotlib.pyplot as plt
plt.get_cmap('tab10', 2)
gives me the first and the last color of the palette
cmap
Is there an easy way to control the order of the colors? My intention is to have a color scheme identical to the matplotlib default for categorical values.

TramineR legend position and axis

I'm working with TraMineR and I don't know how to arrange my plot. So basically what i would like to have the legend under the plot and to remove the space between the x and y axis. Any help is welcomed.
The plot:
Sample code:
seqdplot(Activities.seq, with.legend=FALSE)
legend("bottom", legend=attr(Activities.seq, "labels"),
fill=attr(Activities.seq, "cpal"),
inset=-.1, bty="o", xpd=NA, cex=.75,ncol=3)
The family of seqplot functions offers a series of arguments to control the legend as well as the axes. Look at the help page of seqplot (and of plot.stslist.statd for specific seqdplot parameters).
For instance, you can suppress the x-axis with axes=FALSE, and the y-axis with yaxis=FALSE.
To print the legend you can let seqdplot display it automatically using the default with.legend=TRUE option and control it with for examples cex.legend for the font size, ltext for the text. You can also use the ncol argument to set the number of columns in the legend.
The seqplot functions use by default layout to organize the graphic area between the plots and the legend. If you need more fine tuning (e.g. to change the default par(mar=c(5.1,4.1,4.1,2.1)) margins around the plot and the legend), you should create separately the plot(s) and the legend and then organize them yourself using e.g. layout or par(mfrow=...). In that case, the separate graphics should be created by setting with.legend=FALSE, which prevents the display of the legend and disables the automatic use of layout.
The color legend is easiest obtained with seqlegend.
I illustrate with the mvad data that ships with TraMineR. First the default plot with the legend. Note the use of border=NA to suppress the too many vertical black lines.
library(TraMineR)
data(mvad)
mvad.scode <- c("EM", "FE", "HE", "JL", "SC", "TR")
mvad.seq <- seqdef(mvad, 17:86,
states = mvad.scode,
xtstep = 6)
# Default plot with the legend,
seqdplot(mvad.seq, border=NA)
Now, we suppress the x and y axes and modify the display of the legend
seqdplot(mvad.seq, border=NA,
axes=FALSE, yaxis=FALSE, ylab="",
cex.legend=1.3, ncol=6, legend.prop=.11)
Here is how you can control the space between the plot and the x and y axes
seqdplot(mvad.seq, border=NA, yaxis=FALSE, xaxis=FALSE, with.legend=FALSE)
axis(2, line=-1)
axis(1, line=0)
Creating the legend separately and reducing the left, top, and right margins around the legend
op <- par(mar=c(5.1,0.1,0.1,0.1))
seqlegend(mvad.seq, ncol=2, cex=2)
par(op)

Limited color choice in matplotlib?

Matplotlib is a very good package for 2D plotting in python (It says by using it you can generate production-quality graphs), but when I used it to plot bar charts, I was very disappinted with its limited color resourses. As far as I know, only seven kinds of colors were included, namely blue, cyan, green, black, magenta, red, white and yellow. Can anybody tell me if there are other kinds of colors or just those i listed above?
According to the documentation "In addition [to those seven colors], you can specify colors in many weird and wonderful ways, including full names ('green'), hex strings ('#008000'), RGB or RGBA tuples ((0,1,0,1)) or grayscale intensities as a string ('0.8')." You can do this using the kwarg color.
Here's an example:
from matplotlib.pylab import plot, show, hold
from numpy.random import rand
hold(True)
plot(rand(10,1), color = ((0.1,0.5,0.2)))
plot(rand(10,1), color = '#400000')
plot(rand(10,1), color = '0.8')
show()