Adding Textures to Graphs Using Matplotlib

Prasad Naik
Updated on 16-Mar-2021 11:07:40

593 Views

In this program, we will plot a bar graph using the matplotlib library. The most important Step in solving matplotlib related problems using the matplotlib library is importing the matplotlib library. The syntax is:import matplotlib.pyplot as pltPyplot is a collection of command style functions that make Matplotlib work like MATLAB. In addition to plotting the bar graphs, we will also add some textures to the graphs. The 'hatch' parameter in the bar() function is used to define the texture of the barAlgorithmStep 1: Define a list of values. Step 2: Use the bar() function and define parameters like xaxis, yaxis, ... Read More

Show Two Figures Using Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 11:06:50

10K+ Views

We can use the method, plt.figure(), to create the figures, and then, set their titles by passing strings as arguments.StepsCreate a new figure, or activate an existing figure, with the window title “Welcome to figure 1”.Draw a line using plot() method, over the current figure.Create a new figure, or activate an existing figure, with the window title “Welcome to figure 2”.Draw a line using plot() method, over the current figure.Using plt.show(), show the figures.Examplefrom matplotlib import pyplot as plt plt.figure("Welcome to figure 1") plt.plot([1, 3, 4]) plt.figure("Welcome to figure 2") plt.plot([11, 13, 41]) plt.show()OutputRead More

Displaying Horizontal Bar Graphs Using Matplotlib

Prasad Naik
Updated on 16-Mar-2021 11:06:31

905 Views

In this program, we will plot a bar graph using the matplotlib library. The most important Step in solving matplotlib related problems using the matplotlib library is importing the matplotlib library. The syntax is:import matplotlib.pyplot as pltPyplot is a collection of command style functions that make Matplotlib work like MATLAB. We will use the function barh() for plotting the horizontal bar chartsAlgorithmStep 1: Define a list of values. Step 2: Use the barh() function in the matplotlib.pyplot library and define different parameters like height width, etc. Step 3: Label the axes using xlabel() and ylabel(). Step 3: Plot the graph ... Read More

Plotting a 3D Cube, Sphere and Vector in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 11:06:14

5K+ Views

Get fig from plt.figure() and create three different axes using add_subplot, where projection=3d.Set up the figure title using ax.set_title("name of the figure"). Use the method ax.quiver to plot vector projection, plot3D for cube, and plot_wireframe for sphere after using sin and cos.StepsCreate a new figure, or activate an existing figure.To draw vectors, get a 2D array.Get a zipped object.Add an ~.axes.Axes to the figure as part of a subplot arrangement, with 3d projection, where nrows = 1, ncols = 3 and index = 1.Plot a 3D field of arrows.Set xlim, ylim and zlim.Set the title of the axis (at index ... Read More

Displaying Bar Graphs Using Matplotlib

Prasad Naik
Updated on 16-Mar-2021 11:05:57

308 Views

In this program, we will plot a bar graph using the matplotlib library. The most important Step in solving matplotlib related problems using the matplotlib library is importing the matplotlib library. The syntax is −import matplotlib.pyplot as pltPyplot is a collection of command style functions that make Matplotlib work like MATLABAlgorithmStep 1: Define a list of values. Step 2: Use the bar() function in the matplotlib.pyplot library and define different parameters like height, width, etc. Step 3: Label the axes using xlabel() and ylabel(). Step 3: Plot the graph using show().Example Codeimport matplotlib.pyplot as plt data_x = ['Mumbai', 'Delhi', ... Read More

Display Tick Marks on Upper and Right Side of Plot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:05:36

746 Views

To display tick marks on upper as well as right side of the plot, we can create duplicate axes for X as well Y by using scale_x_continuous and scale_y_continuous functions. The argument that will help us in this case is sec.axis and we need to set it to dup_axis as scale_x_continuous(sec.axis=dup_axis()) and scale_y_continuous(sec.axis=dup_axis()). Check out the below example to understand how it can be done.ExampleConsider the below data frame − Live Demox

Find Length of Words in a Pandas Series

Prasad Naik
Updated on 16-Mar-2021 11:02:49

950 Views

In this task, we will find the length of strings in a Pandas series. We will use the str.len() function in the Pandas library for this purpose.AlgorithmStep 1: Define a Pandas series of string. Step 2: Find the length of each string using the str.len() function. Step 3: Print the results.Example Codeimport pandas as pd series = pd.Series(["Foo", "bar", "London", "Quarantine"]) print("Series: ", series) length = series.str.len() print("Length:", length)OutputSeries: 0           Foo 1           bar 2        London 3    Quarantine dtype: object Length: 0     3 1     3 2     6 3    10 dtype: int64

Changing the Color of an Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 11:02:31

9K+ Views

First, we can get the axes. Then, ax.spines could help to set the color by specifying the name of the axes, i.e., top, bottom, right and left.StepsAdd an axes to the current figure and make it the current axes.Using step 1 axes, we can set the color of all the axes.Using ax.spines[axes].set_color(‘color’), set the color of the axes. Axes could be bottom, top, right, and left. Color could be yellow, red, black, and blue.To show the figure, use the plt.show() method.Examplefrom matplotlib import pyplot as plt ax = plt.axes() ax.spines['bottom'].set_color('yellow') ax.spines['top'].set_color('red') ax.spines['right'].set_color('black') ax.spines['left'].set_color('blue') plt.show()OutputRead More

Divide Each Column by a Particular Column in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:02:17

15K+ Views

To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].ExampleConsider the below data frame − Live Demox1

Convert String of Date into Time using Pandas

Prasad Naik
Updated on 16-Mar-2021 11:02:13

203 Views

In this program, we will convert a date string like "24 August 2020" to 2020-08-24 00:00:00. We will use the to_datetime() function in pandas library to solve this task.AlgorithmStep 1: Define a Pandas series containing date string. Step 2: Convert these date strings into date time format using the to_datetime format(). Step 3: Print the results.Example Codeimport pandas as pd series = pd.Series(["24 August 2020", "25 December 2020 20:05"]) print("Series: ", series) datetime = pd.to_datetime(series) print("DateTime Format: ", datetime)OutputSeries: 0            24 August 2020 1    25 December 2020 20:05 dtype: object DateTime Format: 0   2020-08-24 00:00:00 1   2020-12-25 20:05:00 dtype: datetime64[ns]

Advertisements