Write LaTeX Formula in Plot Legend using Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:29:47

5K+ Views

LaTeX is a typesetting language for producing scientific documents. We use a very small part of the language for writing mathematical notation. Jupyter notebook recognizes LaTeX code written in markdown cells and renders the symbols in the browser using the MathJax JavaScript library.To write a LaTeX formula in the legend of a plot, we can take the following steps −Create data points for x.Create data point for y, i.e., y=sin(x).Plot the curve x and y with LaTex representation.To activate the label, use the legend() method.To display the figure, use the show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Draw Grid Lines Behind Matplotlib Bar Graph

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:29:26

3K+ Views

To draw grid lines behind matplotlib bar graph, we can take the following Steps −Make a list of numbers, i.e., data.Make a bar using the bar() method, by passing data, color='red' and alpha = 0.5. The alpha blending value should be between 0 (transparent) and 1 (opaque).To configure the grid lines, use the grid() method, with color='yellow', linewidth=1, axis='both' and alpha=0.5.To display the figure, show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [3, 5, 9, 15, 12] plt.bar(range(len(data)), data, color='red', alpha=0.5) plt.grid(color='yellow', linewidth=1, axis='both', alpha=0.5) plt.show()OutputRead More

K-th Element of Two Sorted Arrays in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:28:57

190 Views

In this tutorial, we are going to write a program that finds the k-th element from thee merged array of two sorted arrays.Let's see the steps to solve the problem.Initialise the two sorted arrays.Initialise an empty array of size m + n.Merge the two arrays into the new array.Return the k - 1 element from the merged array.ExampleLet's see the code. Live Demo#include using namespace std; int findKthElement(int arr_one[], int arr_two[], int m, int n, int k) {    int sorted_arr[m + n];    int i = 0, j = 0, index = 0;    while (i < m && ... Read More

K-th Digit in a Raised to Power B in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:27:43

338 Views

In this tutorial, we are going to write a program that finds the k-th digit from the right side in the number abIt's a straightforward problem. Let's see the steps to solve it.Initialise the numbers a, b, and k.Find the value of abusing pow method.Write a loop that iterates until power value is less than zero or count is less than k.Get the last digit from the power value.Increment the counter.Check whether k and counter are equal or not.Return the digit if they are equalReturn -1.ExampleLet's see the code. Live Demo#include using namespace std; int getTheDigit(int a, int b, int ... Read More

K Rough Number or K Jagged Number in C++

Hafeezul Kareem
Updated on 09-Apr-2021 12:26:56

243 Views

In this tutorial, we are going to write a program that checks whether the given number is k-rough or k-jagged number or not.The number whose smallest prime factor is greater than or equal to the given k, it is called k-rough or k-jagged number.Let's see the steps to solve the problem.Initialise the numbers n and k.Find all the prime numbers that are factors of n and store them in a vector.Get the first element from the vector and compare it with k to check whether n is k-rough or k-jagged number or not.ExampleLet's see the code. Live Demo#include using namespace ... Read More

Plot Parallel Coordinates in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:20:48

486 Views

To plot parallel coordinates, we can take the following Steps −Load dataset iris using Seaborn (Need internet).Pass the loaded data into the parallel_coordinates() method, which will help in parallel plotting.To display the figure, use the show() method.Exampleimport matplotlib.pyplot as plt from pandas.plotting import parallel_coordinates import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = sns.load_dataset('iris') parallel_coordinates(data, 'species', colormap=plt.get_cmap("Set2")) plt.show()Output

Hide Matplotlib Lines (Line2D) in IPython Notebook

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:20:28

465 Views

To hide matplotlib.lines.Line2D instance while calling the plot() method, we can take the following steps −Import numpy as np.From matplotlib, import pyplot as plt.Create points for x, i.e., np.linspace(1, 10, 1000)Now plot the line using the plot() method.To hide the instance, use plt.plot(x); (with semi colon)Or, use _ = plt.plot(x).ExampleIn [1]: import numpy as np In [2]: from matplotlib import pyplot as plt In [3]: x = np.linspace(1, 10, 1000) In [4]: plt.plot(x) Out[4]: [] In [5]: plt.plot(x); In [6]: _ = plt.plot(x) In [7]:OutputOut[4]: []

Embedding Small Plots Inside Subplots in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:20:06

4K+ Views

To embed small plots inside subplots, we can take the following Steps −Using subplots() method, create a figure and a set of subplots (fig, ax1).On ax1, plot a line with color red, line width=4, label=”outer plot”.Using add_axes(), add an axis, i.e., ax2 with l, b, h and w values.Plot the same points (Step 2) usins the plot() method, with color green, line width=3, label=”inside plot”.Set the legend on both the plots using the legend() method.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax1 = plt.subplots() ax1.plot([1, 4, ... Read More

Change Figure Size Using Seaborn FactorPlot in Matplotlib

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:19:43

406 Views

To change the figuresize using Seaborn factorplot, we can take the following Steps −Load the exercise data using load_dataset() method.Using factorplot() method, change figure size by customising the size and aspect values.To display the figure, use the show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True exercise = sns.load_dataset("exercise") sns.factorplot("kind", "pulse", "diet", exercise, kind="point", size=5, aspect=2) plt.show()Output

Set Font Size of Matplotlib Axis Legend

Rishikesh Kumar Rishi
Updated on 09-Apr-2021 12:19:18

2K+ Views

To set the font size of matplotlib axis legend, we can take the following steps −Create the points for x and y using numpy.Plot x and y using the plot() method with label y=sin(x).Title the plot using the title() method.To set the fontsize, we can override rcParams legend fontsize by value 20.Use the legend() method, and fit the legend at the top-right position.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import matplotlib plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 50) y = np.sin(x) plt.plot(x, y, c="red", lw=7, label="y=sin(x)") ... Read More

Advertisements