Top Seaborn Interview Questions and Answers (2024)
- What is Seaborn?
- What is Matplotlib?
- Does Seaborn need Matplotlib?
- How can we see all of the data sets available in the Seaborn library?
- What is CMAP in Seaborn?
- What is the Seaborn function for colouring plots?
- What is Histograms in Seaborn?
- How do you plot a histogram in Seaborn?
- How to change the minimum number of ticks of MaxNLocator in Seaborn?
- How to put a png image like a watermark on a line plot in Seaborn?
- How to change the legend font size of FacetGrid plot in Seaborn?
- How do I make all of the lines in
seaborn.lineplot
black? - How to color the data points by a category like to assign colors to the 'regional indicators'?
Q: What is Seaborn?
Ans:
Seaborn is an open source, Python data visualisation library built on matplotlib that is tightly integrated with pandas data structures. The core component of Seaborn is visualisation, which aids in data exploration and understanding.
Data can be represented as plots, which are simple to study, explore, and interpret.
Q: What is Matplotlib?
Ans:
Matplotlib is a plotting library for Python with NumPy, the Python numerical mathematics extension. It offers an object-oriented API for embedding plots into applications utilising GUI toolkits such as Tkinter, wxPython, Qt, or GTK.
Q: Does Seaborn need Matplotlib?
Ans:
The only library we need to import is Seaborn. Seaborn's plots are drawn using matplotlib behind the scenes. Seaborn is a library that uses Matplotlib underneath to plot graphs.
- Seaborn helps in resolving the two major issues faced by Matplotlib; the problems are:
- Default Matplotlib parameters
- Working with data frames
Take a look at our Suggested Posts :
Q: How can we see all of the data sets available in the Seaborn library?
Ans:
As mentioned below, the get dataset names() function is used.
import seaborn as sb
print sb.get_dataset_names()
Q: What is CMAP in Seaborn?
Ans:
Sequential Palette : one color only
With the cmap option of the heatmap() method in seaborn, you can adjust the colours of your heatmap. The vmax and vmin parameters in the function can also be used to establish maximum and minimum values for the colour bar on a seaborn heatmap.
Q: What is the Seaborn function for colouring plots?
Ans:
color_palette()
is a Seaborn function that can be used to give colours to plots and give them additional artistic appeal.
Q: What is Histograms in Seaborn?
Ans:
Histograms show the distribution of data by constructing bins throughout the data's range and then drawing bars to show how many observations fall into each bin.
Q: How do you plot a histogram in Seaborn?
Ans:
We can plot a histogram in Seaborn by using histplot()
to plot a histogram with a density plot
.
seaborn.histplot(data, x, y, hue, stat, bins, binwidth, discrete, kde, log_scale)
Q: How to change the minimum number of ticks of MaxNLocator in Seaborn?
Ans:
By default, it have min_n_ticks int: 2
We can update the default min_n_ticks as below:
g.yaxis.set_major_locator(plt.MaxNLocator(3, min_n_ticks=3))
Q: How to put a png image like a watermark on a line plot in Seaborn?
Ans:
We can use below code to place a png image on a line plot.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as image
img = image.imread("test.png") #read image
x = np.arange(0.0, 2.0, 0.01)
y = 1 + np.sin(2 * np.pi * x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='x', ylabel='y', title='Graph Title')
fig.figimage(img, 900, 200, zorder=3, alpha=.3) #900,200 -> x/y image offset in pixels.
plt.show()
Q: How to change the legend font size of FacetGrid plot in Seaborn?
Ans:
We can access the legend from the FacetGrid in which sns.displot
will return with FacetGrid.legend
.
Then you can update the text elements like how we want:
import seaborn as sns
testData = sns.load_dataset("test-example")
gplot = sns.displot(data=testData, x="total_amt", hue="day")
# Legend title
gplot.legend.get_title().set_fontsize(20)
# Legend texts
for text in gplot.legend.texts:
text.set_fontsize(20)
Q: How do I make all of the lines in seaborn.lineplot black?
Ans:
We can set the line colour to black using the line graph as given in the official reference as an example. When there is only one hue, though, it is impossible to distinguish the data. For visualisation, it may be preferable to utilise a single colour tone.
import seaborn as sns
employees = sns.load_dataset("employee")
sns.set_style("whitegrid", {
"ytick.major.size": 0.1,
"ytick.minor.size": 0.05,
'grid.linestyle': 'solid',
})
gplot = sns.lineplot(data=employees, x="year", y="hired", hue='month')
lines = gplot.get_lines()
[l.set_color('black') for l in lines]
gplot.legend()
sns.lineplot(data=flights, x="year", y="hired", hue='month', palette=('Greys'))
Q: How to color the data points by a category like to assign colors to the 'regional indicators'?
Ans:
The simplest solution is to choose the columns and use.melt to reshape them into a long dataframe.
- Then use both together sns.lmplot and sns.regplot.
- Hue can be used to define colours based on region, however this results in a separate regression line for each data point, rather than one for all data points, and as such the regression line is not shown for.lmplot, but is plotted separately for each axis with.regplot.
- seaborn is a high-level API for matplotlib.
- Use pandas 1.2.5, seaborn 0.11.1 and matplotlib 3.4.2.
- The code is reduced from 58 to 13 lines using this implementation.
import pandas as pd
import seaborn as sns
# given dataframe df
data = {'Country name': ['Finland', 'Denmark', 'Switzerland', 'Iceland', 'Netherlands', 'Norway', 'Sweden', 'Luxembourg', 'New Zealand', 'Austria', 'Australia', 'Israel', 'Germany', 'Canada', 'Ireland', 'Costa Rica', 'United Kingdom', 'Czech Republic', 'United States', 'Belgium', 'France', 'Bahrain', 'Malta', 'Taiwan Province of China', 'United Arab Emirates'], 'Generosity': [-0.098, 0.03, 0.025, 0.16, 0.175, 0.093, 0.086, -0.034, 0.134, 0.042, 0.159, 0.031, 0.011, 0.089, 0.077, -0.126, 0.233, -0.208, 0.098, -0.153, -0.147, 0.089, 0.133, -0.07, 0.074], 'Regional indicator': ['Western Europe', 'Western Europe', 'Western Europe', 'Western Europe', 'Western Europe', 'Western Europe', 'Western Europe', 'Western Europe', 'North America and ANZ', 'Western Europe', 'North America and ANZ', 'Middle East and North Africa', 'Western Europe', 'North America and ANZ', 'Western Europe', 'Latin America and Caribbean', 'Western Europe', 'Central and Eastern Europe', 'North America and ANZ', 'Western Europe', 'Western Europe', 'Middle East and North Africa', 'Western Europe', 'East Asia', 'Middle East and North Africa'], 'Social support': [0.954, 0.954, 0.942, 0.983, 0.942, 0.954, 0.934, 0.908, 0.948, 0.934, 0.94, 0.939, 0.903, 0.926, 0.947, 0.891, 0.934, 0.947, 0.92, 0.906, 0.942, 0.862, 0.931, 0.898, 0.844], 'Logged GDP per capita': [10.775, 10.933, 11.117, 10.878, 10.932, 11.053, 10.867, 11.647, 10.643, 10.906, 10.796, 10.575, 10.873, 10.776, 11.342, 9.88, 10.707, 10.556, 11.023, 10.823, 10.704, 10.669, 10.674, 10.871, 11.085], 'Healthy life expectancy': [72.0, 72.7, 74.4, 73.0, 72.4, 73.3, 72.7, 72.6, 73.4, 73.3, 73.9, 73.503, 72.5, 73.8, 72.4, 71.4, 72.5, 70.807, 68.2, 72.199, 74.0, 69.495, 72.2, 69.6, 67.333], 'Perceptions of corruption': [0.186, 0.179, 0.292, 0.673, 0.338, 0.27, 0.237, 0.386, 0.242, 0.481, 0.442, 0.753, 0.46, 0.415, 0.363, 0.809, 0.459, 0.868, 0.698, 0.646, 0.571, 0.722, 0.653, 0.721, 0.589]}
df = pd.DataFrame(data)
# columns to be used as value variables
cols = ['Social support', 'Logged GDP per capita', 'Healthy life expectancy', 'Perceptions of corruption']
# melt the desired columns from dataframe df
dfm = df.melt(id_vars=['Generosity', 'Regional indicator'], value_vars=cols)
# plot the points with color in a FacetGrid
p = sns.lmplot(data=dfm, col='variable', col_wrap=2, col_order=cols, x='value', y='Generosity', hue='Regional indicator', sharey=False, sharex=False, fit_reg=False)
# use regplot to plot the regression line for all points
for i, col in enumerate(cols):
sns.regplot(x=col, y='Generosity', data=df, scatter=False, ax=p.axes[i], ci=False)
# add plot formatting
p.set_titles(row_template='{row_name}', col_template='{col_name}') # shorten the column names
p.fig.suptitle("What Impacts Generosity Around the World?", size=16)
p.fig.subplots_adjust(hspace=.2, wspace=0.2, top=0.9) # add spacing between plots
p.savefig('Generosity.png', dpi=300)