How can I bring color in legends in matplotlib? - dataframe

I have a following code for specific graph, what I want is those legend to be shown in the graph as
Africa: Skyblue,
Europe: Gold,
Americas: Palegreen
How can I have those colors as my legends?

Related

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

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)

axes and labeling not in plot white area

I am trying to make a plot in a Colaboratory notebook. I am using following code:
# Plot data
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(12,8))
plt.title("Charleston", size=24)
plt.xlabel("Year", size=20)
plt.ylabel("High Tide Floods (days/year)", size=20)
plt.plot(HTF_yr.index, HTF_yr.numdays, color='black',marker ='o')
plt.show()
Result (below) displays axes and labels in a grey area. How do I include them in the white area, i.e. the figure itself?

How can I remove the edges from the hist2d

I plot the 2D histogram using hist2d in matplotlib. I want to remove the unwanted black edges. How can I do that? These edges appear when I add alpha=0.5 to hist2d.

Seaborn chart colors are different than those specified by palette

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.