Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to label and change the scale of Seaborn kdeplot's axes

Here's my code

import numpy as np
from numpy.random import randn
import pandas as pd
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns


fig = sns.kdeplot(treze, shade=True, color=c1,cut =0, clip=(0,2000))
fig = sns.kdeplot(cjjardim, shade=True, color=c2,cut =0, clip=(0,2000))

fig.figure.suptitle("Plot", fontsize = 24)

plt.xlabel('Purchase amount', fontsize=18)
plt.ylabel('Distribution', fontsize=16)

, which results in the following plot:

enter image description here

I want to do two things:

1) Change the scale of the y-axis by multiplying its values by 10000 and, if it's possible, add a % sign to the numbers. In other words, I want the y-axis values shown in the above plot to be 0%, 5%, 10%, 15%, 20%, 25%, and 30%.

2) Add more values to the x-axis. I'm particularly interested in showing the data in intervals of 200. In other words, I want the x-axis values shown in the plot to be 0, 200, 400, 600,... and so on.

like image 814
iatowks Avatar asked Jun 13 '15 13:06

iatowks


People also ask

How do I change my axis title in Seaborn?

We make use of the set_title(), set_xlabel(), and set_ylabel() functions to change axis labels and set the title for a plot. We can set the size of the text with size attribute.

How do you set Xticks in Seaborn?

set_xticks() and Axes. set_yticks() functions in axes module of matplotlib library are used to Set the ticks with a list of ticks on X-axis and Y-axis respectively. Parameters: ticks: This parameter is the list of x-axis/y-axis tick locations.

What is Kdeplot in Seaborn?

Kdeplot is a Kernel Distribution Estimation Plot which depicts the probability density function of the continuous or non-parametric data variables i.e. we can plot for the univariate or multiple variables altogether. Using the Python Seaborn module, we can build the Kdeplot with various functionality added to it.


1 Answers

1) what you are looking for is most probably some combination of get_yticks() and set_yticks:

plt.yticks(fig.get_yticks(), fig.get_yticks() * 100)
plt.ylabel('Distribution [%]', fontsize=16)

Note: as mwaskom is commenting times 10000 and a % sign is mathematically incorrect.

2) you can specify where you want your ticks via the xticks function. Then you have more ticks and data get easier to read. You do not get more data that way.

plt.xticks([0, 200, 400, 600])
plt.xlabel('Purchase amount', fontsize=18)

Note: if you wanted to limit the view to your specified x-values you might even have a glimpse at plt.xlim() and reduce the figure to the interesting range.

like image 109
cattt84 Avatar answered Sep 19 '22 08:09

cattt84