Change Tick Size Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:31:50

3K+ Views

To change the tick size using ggplot2, we can use theme function with argument axis.ticks.length. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with larger size of tick marks can be created by using the below command −ggplot(df,aes(x,y))+geom_point()+theme(axis.ticks.length=unit(0.8,"inch"))ExampleConsider the below data frame − Live Demox

Combine Data Frame and Named Vector in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:28:47

876 Views

If we have a data frame that contains a character column and a named vector which has the same names as in the character column of the data frame then we can combine this data frame and the vector by using match function be appropriately defining the names and the character column. Check out the below example to understand how it can be done.ExampleConsider the below data frame df1 and the vector v1 − Live Demodf1

Change Values on Matplotlib Imshow Graph Axis

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:21:58

4K+ Views

First, we can initialize an array matrix and pass it into the imshow method that can help to get the image for the given matrix.StepsCreate a 2D Array i.e., img.Using imshow() method, display the data as an image, i.e., on a 2D regular raster.Use plt.show() method to show the figure.Exampleimport matplotlib.pyplot as plt img = [[1, 2, 4, 5, 6, 7],       [11, 12, 14, 15, 16, 17],       [101, 12, 41, 51, 61, 71],       [111, 121, 141, 151, 161, 171]] plt.imshow(img, extent=[0, 5, 0, 5]) plt.show()Output

Generate Random Colors in Matplotlib

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

3K+ Views

To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass into the scatter method to get the desired output.StepsTake an input from the user for the number of colors, i.e., number_of_colors = 20.Use Hexadecimal alphabets to get a color.Create a color from (step 2) by choosing a random character from step 2 data.Plot scatter points for step 1 input data, with step 3 colors.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt import random number_of_colors = int(input("Please enter number of colors: ")) hexadecimal_alphabets ... Read More

Create NumPy Array Within a Given Range

Prasad Naik
Updated on 16-Mar-2021 10:21:08

1K+ Views

We have to create a numpy array in the range provided by the user. We will use the arange() function in the numpy library to get our output.AlgorithmStep1: Import numpy. Step 2: Take start_value, end_value and Step from the user. Step 3: Print the array using arange() function in numpy.Example Codeimport numpy as np start_val = int(input("Enter starting value: ")) end_val = int(input("Enter ending value: ")) Step_val = int(input("Enter Step value: ")) print(np.arange(start_val, end_val, Step_val))OutputEnter starting value: 5 Enter ending value: 50 Enter Step value: 5 [ 5 10 15 20 25 30 35 40 45]

Convert Number to Words in R Data Frame Column

Nizamuddin Siddiqui
Updated on 16-Mar-2021 10:21:07

4K+ Views

To convert number to words in an R data frame column, we can use english function from english package. For example, if we have a data frame called df that contains a number column x then we can convert the numbers into words by using the command as.character(english(df$x)).ExampleConsider the below data frame − Live Demox

Create Identity Matrix Using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:20:45

4K+ Views

In this program, we will print an identity matrix of size nxn where n will be taken as an input from the user. We shall use the identity() function in the numpy library which takes in the dimension and the data type of the elements as parametersAlgorithmStep 1: Import numpy. Step 2: Take dimensions as input from the user. Step 3: Print the identity matrix using numpy.identity() function.Example Codeimport numpy as np dimension = int(input("Enter the dimension of identitiy matrix: ")) identity_matrix = np.identity(dimension, dtype="int") print(identity_matrix)OutputEnter the dimension of identitiy matrix: 5 [[1 0 0 0 0]  [0 1 0 0 0]  [0 0 1 0 0]  [0 0 0 1 0]  [0 0 0 0 1]]

Find Number of Rows and Columns in a Matrix Using NumPy

Prasad Naik
Updated on 16-Mar-2021 10:20:19

3K+ Views

We will first create a numpy matrix and then find out the number of rows and columns in that matrixAlgorithmStep 1: Create a numpy matrix of random numbers. Step 2: Find the rows and columns of the matrix using numpy.shape function. Step 3: Print the number of rows and columns.Example Codeimport numpy as np matrix = np.random.rand(2,3) print(matrix) print("Total number of rows and columns in the given matrix are: ", matrix.shape)Output[[0.23226052 0.89690884 0.19813164]  [0.85170808 0.97725669 0.72454096]] Total number of rows and columns in the given matrix are:  (2, 3)

Custom Colors in Pandas Matplotlib Bar Graph

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:19:16

4K+ Views

To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass them into the scatter method to get the desired output.Using the set_color method, we could set the color of the bar.StepsTake user input for the number of bars.Add bar using plt.bar() method.Create colors from hexadecimal alphabets by choosing random characters.Set the color for every bar, using set_color() method.To show the figure we can use plt.show() method.Examplefrom matplotlib import pyplot as plt import random bar_count = int(input("Enter number of bars: ")) bars = plt.bar([i for ... Read More

Change Backends in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:18:47

2K+ Views

We can override the backend value using atplotlib.rcParams['backend'] variable.StepsUsing get_backend() method, return the name of the current backend, i.e., default name.Now override the backend name.Using get_backend() method, return the name of the current backend, i.e., updated name.Exampleimport matplotlib print("Before, Backend used by matplotlib is: ", matplotlib.get_backend()) matplotlib.rcParams['backend'] = 'TkAgg' print("After, Backend used by matplotlib is: ", matplotlib.get_backend())OutputBefore, Backend used by matplotlib is: GTK3Agg After, Backend used by matplotlib is: TkAgg Enter number of bars: 5

Advertisements