To draw a filled arc in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Initialize two variables, r, yoff.Create x and y data points using Numpy.Fill the area between x and y plots.Set the axis aspect and draw the figure canvas.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fg, ax = plt.subplots(1, 1) r = 2. yoff = -1 x = np.arange(-1., 1.05, 0.05) y ... Read More
To create a surface plot from a grayscale image with matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create random data points using Numpy.Get the xx and yy data points from a 2d image data raster.Create a new figure or activate an existing figure.Get the current axis of the plot and make it 3d projection axes.Create a surface plot with cmap='gray'.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) xx, ... Read More
To remove the first and last ticks label of each Y-axis subplot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Iterate the axes and set the first and last ticklabel's visible=False.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots(2, sharex=True) for a in ax: plt.setp(a.get_yticklabels()[0], visible=False) plt.setp(a.get_yticklabels()[-1], visible=False) plt.show()OutputRead More
To extract only the month and day from a datetime object in Python, we can use the DateFormatter() class.stepsSet the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Create a figure and a set of subplots.Plot the dataframe using plot() method.Set the axis formatter, extract month and day.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt, dates plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) fig, ax = ... Read More
To remove whitespaces at the bottom of a Matplotlib graph, we can use tight layout or autoscale_on=False.stepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Plot a list of data points using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, autoscale_on=False, xlim=(1, 5), ylim=(0, 10)) ax.plot([2, 5, 1, 2, 0, 7]) plt.show()OutputRead More
To understand Seaborn's heatmap annotation format, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with five columns.Plot the rectangular data as a color-encoded matrix, fmt=".2%" represents the annotation format.To display the figure, use show() method.ExampleExampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns.heatmap(df, annot=True, annot_kws={"size": 7}, fmt=".2%") plt.show()OutputRead More
To create a 100% stacked Area Chart with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of years.Make a dictionary, with list of population in respective years.Create a figure and a set of subplots.Draw a stacked Area Plot.Place a legend on the figure, at the location ''upper left''.Set the title, xlabel and ylabel.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018] population_by_continent = { ... Read More
To stack a multi-level column, use the stack() method. At first, import the required library −import pandas as pdCreate a multi-level column −items = pd.MultiIndex.from_tuples([('Maths', 'Mental Maths'), ('Maths', 'Discrete Mathematics'), ('Maths', 'Applied Mathematics')]) Now, create a DataFrame and set multi-level columns we set above −dataFrame = pd.DataFrame([[67, 86, 78], [56, 92, 97], [92, 95, 91]], index=['John', 'Tom', 'Henry'], columns=items)Stack the multi-level column −dataframe.stack()ExampleFollowing is the complete code −import pandas as pd # multi-level columns items = pd.MultiIndex.from_tuples([('Maths', 'Mental Maths'), ('Maths', 'Discrete Mathematics'), ('Maths', 'Applied Mathematics')]) # creating a DataFrame dataFrame = pd.DataFrame([[67, 86, 78], [56, 92, 97], ... Read More
To create a subset and display only the last entry from duplicate values, use the “keep” parameter with the ‘last” value in drop_duplicates() method. The drop_duplicates() method removed duplicates.Let us first create a DataFrame with 3 columns −dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [85, 70, 80, 95, 55, 90]})Removing duplicates and displaying last entry. Using keep parameter, we have set "last". Duplicate rows except the last entry will get deleted. We have considered a subset using the “subset” parameter −dataFrame2 = dataFrame.drop_duplicates(subset = ['Car', 'Place'], keep ='last').reset_index(drop = True)ExampleFollowing ... Read More
To get the columns shared by two DataFrames, use the intersect1d() method. This method is provided by numpy, so you need to import Numpy also with Pandas. Let us first import the required libraries −import pandas as pd import numpy as npCreate two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 110, 150, 80, 200, 90] }) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP