Found 26504 Articles for Server Side Programming

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

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

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

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

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

Python program to reverse a Numpy array?

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

614 Views

This is a simple program wherein we have to reverse a numpy array. We will use numpy.flip() function for the same.AlgorithmStep 1: Import numpy. Step 2: Define a numpy array using numpy.array(). Step 3: Reverse the array using numpy.flip() function. Step 4: Print the array.Example Codeimport numpy as np arr = np.array([10,20,30,40,50]) print("Original Array: ", arr) arr_reversed = np.flip(arr) print("Reversed Array: ", arr_reversed)OutputOriginal Array: [10 20 30 40 50] Reversed Array: [50 40 30 20 10]

How can I plot NaN values as a special color with imshow in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:42:30

1K+ Views

First, we can create an array matrix with some np.nan value, and using imshow method, we can create a diagram for that matrix.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, nrows = 1, ncols = 1, index = 1.Create a 2D array with np.nan.Display data as an image, i.e., on a 2D regular raster.Use the draw() method which draws the drawing at the given location.To show the figure, use the plt.show() method.Exampleimport numpy as np import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot(111) a = ... Read More

How to convert the row values in a matrix to row percentage in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:38:16

901 Views

To convert the row values in a matrix to row percentage, we can find the row sums and divide each row value by this sum. For example, if we have a matrix called M then we can convert the row values in M to row percentage by using the commandround((M/rowSums(M))*100,2)ExampleConsider the below matrix − Live DemoM1

How to get a matplotlib Axes instance to plot to?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:41:32

535 Views

To get the axes instance, we will use the subplots() method.StepsMake a list of years.Make a list of populations in that year.Get the number of labels using np.arrange(len(years)) method.Set the width of the bars.Create fig and ax variables using subplots() method, where default nrows and ncols are 1.Set the Y-axis label of the figure using set_ylabel().Set the title of the figure, using set_title() method.Set the x-ticks with x that is created in step 3, using set_xticks method.Set the xtick_labels with years data, using set_xticklabels method.Use plt.show() method to show the figure.Examplefrom matplotlib import pyplot as plt import numpy as np ... Read More

How to force the Y axis to use only integers in Matplotlib?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:37:29

7K+ Views

Whenever Y value list will be made, then we will convert those datasets into a new list, with ceil and floor value of the given list accordingly. Then, we can plot the graph for the new list data.StepsTake an input list.Find the minimum and maximum values in the input list (Step 1).Create a range between min and max value (Step 2).Get or set the current tick locations and labels of the Y-axis, with a new list.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Set a title for the axes.To show the figure we can use the ... Read More

Advertisements