Login
Congrats in choosing to up-skill for your bright career! Please share correct details.
Home / Blog / Interview Questions / Top 20+ Python Libraries Interview Questions & Answers
Bharani Kumar Depuru is a well known IT personality from Hyderabad. He is the Founder and Director of AiSPRY and 360DigiTMG. Bharani Kumar is an IIT and ISB alumni with more than 17 years of experience, he held prominent positions in the IT elites like HSBC, ITC Infotech, Infosys, and Deloitte. He is a prevalent IT consultant specializing in Industrial Revolution 4.0 implementation, Data Analytics practice setup, Artificial Intelligence, Big Data Analytics, Industrial IoT, Business Intelligence and Business Management. Bharani Kumar is also the chief trainer at 360DigiTMG with more than Ten years of experience and has been making the IT transition journey easy for his students. 360DigiTMG is at the forefront of delivering quality education, thereby bridging the gap between academia and industry.
Table of Content
Answer - d) legend()
The legends function will be used for automatic generate of figure legends.matplotlib.pyplot.legend([“blue”, “green”], bbox_to_anchor=(0.75, 1.15), ncol=2). It is an area whicg describes the elements of the graph.In the matplotlib library, we have a function called legend() which helps us to Place a legend on the axes.The Following are some more attributes of function legend() : shadow: [None or bool] Whether to draw a shadow behind the legThe legends function will be used for the automatic generation of figure legends.matplotlib.pyplot.legend([“blue”, “green”], bbox_to_anchor=(0.75, 1.15), ncol=2). It is an area that describes the elements of the graph. In the matplotlib library, we have a function called legend() which helps us to Place a legend on the axes. The Following are some more attributes of function legend() : shadow: [None or bool] Whether to draw a shadow behind the legend. Default =None. marker scale: [None or int or float] The relative size of legend markers compared with the originally drawn ones. Default =None. num points: [None or int] The number of marker points in the legend when creating a legend entry for a Line2D (line). Default = None. font-size: The font size of the legend. If the value is numeric the size will be the absolute font size in points. face color: [None or color] The legend’s background color. edge color: [None or color] The legend’s background patch edge color.end. Default =None. markerscale: [None or int or float] The relative size of legend markers compared with the originally drawn ones. Default =None. numpoints: [None or int] The number of marker points in the legend when creating a legend entry for a Line2D (line). Default = None. fontsize: The font size of the legend.If the value is numeric the size will be the absolute font size in points. facecolor: [None or color] The legend’s background color. edgecolor: [None or color] The legend’s background patch edge color.
Answer - d) imshow()
yes, using matplotlib we can display the images in python using imshow() function. The imshow() function in pyplot module of matplotlib library is used to display data as an image; i.e. on a 2D format.syntax:matplotlib.pyplot.imshow(X, cmap=None, norm=None, *, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, interpolation_stage=None, filternorm=True, filterrad=4.0, resample=None, url=None, data=None, **kwargs).wordcloud_ip = WordCloud( background_color='White', width=1800, height=1400 ).generate(ip_rev_string) plt.imshow(wordcloud_ip) . To build a word cloud from bunch of words we can pass the height and width , give a different background do the changes accordingly to generate a cloud of words.
Answer - c) geoplotlib
Geoplotlib is a toolbox for creating maps and plotting geographical data. You can use it to create a variety of map types, like choropleths, heatmaps, and dot-density maps. We can plot 2D histograms and area maps as well. Some of the modules are the Geoplotlib module geoplotlib.layers module geoplotlib.utils module geoplotlib.core module geoplotlib.colors module. It is built on 3 key factors:1)It is very simple 2)Integration 3)performance. It also performs map rendering. The prerequisites to use this are NumPy and SciPY packages for numerical calculations and pyglet packages for graphical rendering.
Answer - b) Subplot()
import numpy as np import matplotlib.pyplot as plt x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1, 1) plt.plot(x1, y1, 'o-') plt.title('A tale of 2 subplots') plt.ylabel('Damped oscillation') plt.subplot(2, 1, 2) plt.plot(x2, y2, '.-') plt.xlabel('time (s)') plt.ylabel('Undamped') plt.show() ""Multiple axes can be created using subplot() function in Matplotlib package.
Answer - a) stats.levene()
stats.levene() is the function name to perform variance test in Python where it will check the data has the variation in the data or not but give through p value, if p value is less than 0.05 the data is not having equal variance and p value is greater than 0.05 then the data is having equal variance when H0 is consider as data is normal and H1 considered as data is not normal. syntax:scipy.stats.levene(*args, center='median', proportiontocut=0.05)[source] Perform Levene test for equal variances. eg:from scipy.stats import levene x = [8.88, 9.12, 9.04, 8.98, 9.00, 9.08, 9.01, 8.85, 9.07, 8.99] y = [8.88, 8.95, 9.28, 9.44, 9.15, 9.58, 8.36, 9.18, 8.67, 9.05] z = [8.95, 9.12, 8.95, 8.84, 9.03, 8.84, 9.06, 8.98, 8.87, 8.98] stat, p = levene(x, y, z) p output:0.002431505967249681 Interpretation: With small p-value we conclude that the populations do not have equal variances.
Answer - c) Altair
Altair uses a declarative style in creating plots, it becomes very easy and quick to repeat through visualizations and experiments at a fast pace when using this library. Here declarative means while plotting any chart, we declare connection between columns to the encoding channels, such as x-axis, y-axis, etc., and the rest related to plot details are handled automatically. we can do aggregation and filtering and dynamic filtering are the cool feature of python Altair. It gives a heat map that uses a text mark attribute. The code framework remains the same but changes in mark attribute can produce different plots. It supports line charts, stacked barplot.
Answer - c) Streamplot()
The streamplot function is used to draw streamlines for vector fields, you will also have control of colour and width of the line. It helps the physicists to plot fluid flow and 2D field ingradients. Syntax: ax.streamplot(X, Y, u,v density=spacing) # Import libraries import numpy as np import matplotlib.pyplot as plt # Creating dataset x = np.arange(0, 5) y = np.arange(0, 5) # Creating grids X, Y = np.meshgrid(x, y) # x-component to the right u = np.ones((5,5)) # y-component zero v = np.zeros((5, 5)) fig = plt.figure(figsize = (12, 7)) # Plotting stream plot plt.streamplot(X, Y, u, v, density = 0.5) # show plot plt.show() X and Y are 1D arrays on an evenly spaced grid,u and v are 2D arrays of velocities of x and y,density is a float value which controls the closeness of the stream lines.
Answer - d) All the above
All the three functions will be used to create logarithmic plots like semiology, semilogx, loglog base 2 on x, and error bar negative. import matplotlib.pyplot as plt # defining the values # at X and Y axis x = [1, 4, 5, 8, 5, 7] y = [300, 400, 300, 400, 500, 800] # plotting the given graph plt.semilogx(x, y, marker = ""."", markersize = 15, color = ""green"") # plot with grid plt.grid(True) # show the plot plt.show()
We have different kinds of categorical distribution plots, box plots, and violin plots. These kinds of plots allow us to choose a numerical variable, like weight, and plot the distribution of weight for each category in a selected categorical variable. It is a function available in the seaborn package. We can use cat plot to combine boxplot and facet grid.syntax:sns.catplot(x="category", y="continuous", hue="smoker", kind="box", data=df)
Answer - a) True
We can embed our graphs using this library.plotly. plot() create public graphs . This is free. With a subscription, you can easily make charts private or secret via the sharing argument. The plots are generated using GUI. This helps in web-based data visualizations. we can make 3D graph,density heatmap,tree map etc. import plotly.express as px fig = px.imshow([[1, 20, 30], [20, 1, 60], [30, 60, 1]]) fig.show()
Answer - a) Marginal Plots
Marginal plots are used to find the relationship between x and y and x and x and observe their distributions. Such scatter plots that have histograms, box plots, or dot plots in the margins of respective x and y axes. Seaborn gives the marginal plots with seaborn sns.joint plot(x=df["sepal_length"], y=df["sepal_width"], kind='scatter') Using Plotly also these plots are also done.
Answer - a) Yes
We can embed bokeh documents into web pages.These applications require a Bokeh server to work. Having a Bokeh server helps us in connecting events and tools to real-time Python callbacks that execute on the server.Bokeh. json, bokeh html , bokeh serve helps in rendering.bokeh.models which is a low level interface that provides high flexibility to application developers. from bokeh.plotting import figure, output_notebook, show # output to notebook output_notebook() # create figure p = figure(plot_width = 400, plot_height = 400) # add a line renderer p.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5], line_width = 2, color = "green") # show the results show(p).
Answer - a) sns.displot with FacetGrid.legend
We can access the legend from the FacetGrid in which sns.displot will return with FacetGrid.legend. 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)
Answer - a) Atlair
Altair's API is easy to understand. Unlike Matplotlib, it's declarative: you only need to specify the links between the data columns to the encoding channels, and the rest of the plotting is handled automatically. This sounds abstract but is a big deal when you are working with data, and it makes visualizing information fast and intuitive. syntax:import seaborn as sns import Altair as alt titanic = sns.load_dataset("titanic") alt.Chart(titanic).mark_bar().encode( x='class', y='count()' )
Answer - a) Pie
Pie charts can be used to identify proportions of the different components in a given whole.
Answer - c) Bootstrap
Bootstrap plot is the result for the combination of resulting plots and histograms.
Answer - a) spread of data
The histogram helps in observing the spread of data and the secondary purpose of the plot is outlier detection. This is available in matplotlib package.import matplotlib .pyplot as plt plt.hist()
We can calculate the uncertainty of a statistic of a population mathematically, using confidence intervals. The bootstrap plot gives an estimation of the required information from the population, not the exact values.
Answer - d) No error
The code has no error. The code is written is for barplot. the barplot holds good for categorical data .matplotlib package is best for basic visualization. This is a univariate plot where we have the variables on the X-axis and y being data.
Answer - a) Histogram
The primary purpose of Histogram gives the
Answer - a) Stripplot
It is a complement to boxplot and violin plot .It is used to draw scatterplot for categorical.Syntax: seaborn.stripplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor=’gray’, linewidth=0, ax=None, **kwargs)
Plotly graph objects are a high-level interface to plotly which are easy to use. It can plot various types of graphs and charts like scatter plots, line charts, bar charts, box plots, histograms, pie charts, etc.
FacetGrid class helps in visualizing the distribution of one variable as well as the relationship between multiple variables separately within subsets of your dataset using multiple panels.
Answer - a) plt.hist(x, bins = number of bins)
The histogram plot is observed by using a package called matplotlib. plt.hist(). SVG histogram, animated histogram,2D histogram with rectangular bins. The histogram by default calculates bins using math calculations. The other way round we can specify bins of your choice.
Answer - c) 1 follows 2
To create a dataframe from lists ,1)create an empty dataframe2)add lists as individuals columns to the list. df=pd.DataFrame() bikes=["bajaj","tvs","herohonda","kawasaki","bmw"] cars=["lamborghini","masserati","ferrari","hyundai","ford"] df["cars"]=cars df["bikes"]=bikes df. Dataframe canbe done using list with index and column names,Using zip() for zipping two lists,Creating DataFrame using multi-dimensional list,Using lists in dictionary to create dataframe. Calling DataFrame constructor after zipping both lists, with columns specified df = pd.DataFrame(list(zip(lst, lst2)), columns =['Name', 'val'])
Answer - d) All the Above
To create zeros with numpy array is:numpy.zeros(shape, dtype=float, order='C') shape= any integer value .To create a two-dimensional array of zeros, pass the shape i.e., number of rows and columns as the value to shape parameter.we can create numpy zeros array with specific datatype, pass the required datatype as dtype parameter.np.zeros((2, 1)) import numpy as np array_mix_type = np.zeros((2, 2), dtype=[('x', 'int'), ('y', 'float')]) print(array_mix_type) print(array_mix_type.dtype) to get the output with desired data type
360DigiTMG - Data Analytics, Data Science Course Training Hyderabad
2-56/2/19, 3rd floor, Vijaya Towers, near Meridian School, Ayyappa Society Rd, Madhapur, Hyderabad, Telangana 500081
099899 94319
Didn’t receive OTP? Resend
Let's Connect! Please share your details here