Found 33676 Articles for Programming

How can I make a 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

Comparing two Pandas series and printing the 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

Print the standard deviation of Pandas series

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

378 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

How to find the correlation matrix with p-values for an R data frame?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:51:59

10K+ Views

The correlation matrix with p-values for an R data frame can be found by using the function rcorr of Hmisc package and read the output as matrix. For example, if we have a data frame called df then the correlation matrix with p-values can be found by using rcorr(as.matrix(df)).ExampleConsider the below data frame − Live Demodf1

How to 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

How can I plot a histogram such that the heights of the bars sum to 1 in matplotlib?

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

593 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

How to 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

Pandas timeseries plot setting X-axis major and minor ticks and labels

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

454 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

How to sort a Pandas Series?

Prasad Naik
Updated on 16-Mar-2021 10:42:52

329 Views

In this problem we have to sort a Pandas series. We will define an unsorted pandas series and will sort it using the sort_values() function in the Pandas library.AlgorithmStep 1: Define Pandas series. Step 2: Sort the series using sort_values() function. Step 3: Print the sorted series.Example Codeimport pandas as pd panda_series = pd.Series([18, 15, 66, 92, 55, 989]) print("Unsorted Pandas Series: ", panda_series) panda_series_sorted = panda_series.sort_values(ascending = True) print("Sorted Pandas Series: ", panda_series_sorted)OutputUnsorted Pandas Series: 0     18 1     15 2     66 3     92 4     55 5   ... Read More

Advertisements