Remove Lines in a Matplotlib Plot

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:44

9K+ Views

We will create two lines, i.e., line1 and line2. After that, we will pop the second line and will remove it.StepsCreate lists for line1 and line2.Plot line1 and line 2 using plot() method, line 2 with style =’dashed’.Set or retrieve auto-scaling margins(0.2).Pop line 2, and remove it using remove() method.The final figure will have only one line, so use plt.show() method.Exampleimport matplotlib.pyplot as plt line1 = [2, 4, 8] line2 = [3, 6, 12] plt.plot(line1) line_2 = plt.plot(line2, linestyle='dashed') plt.margins(0.2) plt.title("With extra lines") plt.show() plt.plot(line1) l = line_2.pop(0) l.remove() plt.margins(0.2) plt.title("Removed extra lines") plt.show()Input Diagram (Before ... Read More

X-ticks in Subplots using Python

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:50:15

5K+ Views

Subplot can split the figure in nrow*ncols parts and plt.xticks could help to plot the xticks for subplots.StepsCreate two lists for line 1 and line 2.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 1.Draw line 1 with style as dashed.Set or retrieve auto scaling margins(0.2).Place xticks at even places.Set a title for the X-axis.Add a subplot to the current figure, nrow = 1, ncols = 2 and index = 2.Plot line 2.Set or retrieve auto scaling margins(0.2).Place xticks at odd places.Set a title for the X-axis.To show the figure use plt.show() method.Exampleimport ... Read More

Compare Two Pandas Series and Print the Difference

Prasad Naik
Updated on 16-Mar-2021 10:49:44

3K+ Views

In this program, we will compare two Pandas series and will print the differences in the series. By difference, we mean that the index positions at which the elements did not match.AlgorithmStep 1: Define two Pandas series, s1 and s2. Step 2: Compare the series using compare() function in the Pandas series. Step 3: Print their difference.Example Codeimport pandas as pd s1 = pd.Series([10, 20, 30, 40, 50, 60]) s2 = pd.Series([10, 30, 30, 40, 55, 60]) print("S1:", s1) print("S2:", s2) difference = s1.compare(s2) print("Difference between the series: ", difference)OutputS1: 0    10 1    20 2 ... Read More

Scatter Plot Colored by Density in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:49:08

2K+ Views

We can create a dict for color and a value. If the same value comes up, we can use a scatter method and if the closer values have the same set of colors, that could make the plot color denser.StepsCreate a new figure, or activate an existing figure.Add an ~.axes.Axes to the figure as part of a subplot arrangement.Get the x and y values using np.random.normal() method. Draw random samples from a normal (Gaussian) distribution.Make a color list with red and blue colors.To make it denser, we can store the same color with the same value.Plot scatter point, a scatter ... Read More

Print the Standard Deviation of Pandas Series

Prasad Naik
Updated on 16-Mar-2021 10:48:04

376 Views

In this program, we will find the standard deviation of a Pandas series. Standard deviation is a statistic that measures the dispersion of a dataset relative to its mean and is calculated as the square root of the variance.AlgorithmStep 1: Define a Pandas series Step 2: Calculate the standard deviation of the series using the std() function in the pandas library. Step 3: Print the standard deviation.Example Codeimport pandas as pd series = pd.Series([10,20,30,40,50]) print("Series: ", series) series_std = series.std() print("Standard Deviation of the series: ",series.std())OutputSeries: 0    10 1    20 2    30 3    40 4    50 dtype: int64 Standard Deviation of the series:  15.811388300841896

Print the Mean of a Pandas Series

Prasad Naik
Updated on 16-Mar-2021 10:47:48

1K+ Views

mean() function in the Pandas library can be used to find the mean of a series.AlgorithmStep 1: Define a Pandas series. Step 2: Use the mean() function to calculate the mean. Step 3: Print the mean.Example Codeimport pandas as pd series = pd.Series([10,20,30,40,50]) print("Pandas Series: ", series) series_mean = series.mean() print("Mean of the Pandas series:", series_mean)OutputPandas Series: 0    10 1    20 2    30 3    40 4    50 dtype: int64 Mean of the Pandas series: 30.0

Create Horizontal Lines for Each Bar in a Bar Plot of Base R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:44:37

2K+ Views

To create horizontal lines for each bar in a bar plot of base R, we can use abline function and pass the same values as in the original barplot with h argument that represents horizontal with different color to make the plot a little better in terms of visualization.Example Live Demox

Set X-axis Major and Minor Ticks and Labels in Pandas Time Series Plot

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:44:02

451 Views

Using Pandas, we can create a dataframe with time and speed, and thereafter, we can use the data frame to get the desired plot.StepsConstruct a new Generator with the default BitGenerator (PCG64).Using Pandas, get a fixed frequency DatetimeIndex. From '2020-01-01' to '2021-01-01'.Draw samples from a log-normal distribution.Make a data frame with above data.Using panda dataframe create plot, with figsize = (10, 5).To show the figure, use the plt.show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt rng = np.random.default_rng(seed=1) date_day = pd.date_range(start='2020-01-01', end='2021-01-01', freq='D') traffic = rng.lognormal(sigma=2, size=date_day.size) df_day = pd.DataFrame(dict(speed=[pow(2, -i) for ... Read More

Plot Histogram with Heights Summing to 1 in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:43:44

587 Views

In plt.hist() method, stacked=True could help to get the heights of the bars sum to 1.StepsCreate a list of numbers.Using plt.hist(), we can draw the histogram.stacked : bool, default: FalseIf "True", multiple data are stacked on top of each other If ``False`` multiple data are arranged side by side if histtype is 'bar' or on top of each other if histtype is 'step'.density : bool, default: FalseIf "True", draw and return a probability density: each bin will display the bin's raw count divided by the total number of counts *and the bin width*.To show the figure, use the plt.show() method.Examplefrom ... Read More

Append Elements to a Pandas Series

Prasad Naik
Updated on 16-Mar-2021 10:43:12

16K+ Views

In this program, we will append elements to a Pandas series. We will use the append() function for this task. Please note that we can only append a series or list/tuple of series to the existing series.AlgorithmStep1: Define a Pandas series, s1. Step 2: Define another series, s2. Step 3: Append s2 to s1. Step 4: Print the final appended series.Example Codeimport pandas as pd s1 = pd.Series([10, 20, 30, 40, 50]) s2 = pd.Series([11, 22, 33, 44, 55]) print("S1:", s1) print("S2:", s2) appended_series = s1.append(s2) print("Final Series after appending:", appended_series)OutputS1: 0    10 1    20 ... Read More

Advertisements