Programming Articles - Page 1317 of 3368

C++ program to find Nth term of the series 1, 5, 32, 288 …

sudhir sharma
Updated on 15-Mar-2021 10:38:54

349 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,5, 32, 288 ...Let’s take an example to understand the problem,InputN = 4Output288Explanation4th term − (4^4) + (3^3) + (2^2) + (1^1) = 256 + 27 + 4 + 1 = 288Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = ( N^N ) + ( (N-1)^(N-1) ) + … + ( 2^2 ) + ( 1^1 )Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    if (N

C++ program to Find Nth term of the series 1, 1, 2, 6, 24…

sudhir sharma
Updated on 15-Mar-2021 10:37:36

533 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,1, 2, 6, 24, ...Let’s take an example to understand the problem,InputN = 7Output720ExplanationThe series is − 1, 1, 2, 6, 24, 120, 720Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = (N−1)!Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    if (N

Change x axes scale in matplotlib

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:44:15

15K+ Views

Using plt.xticks, we can change the X-axis scale.StepsUsing plt.plot() method, we can create a line with two lists that are passed in its argument.Add text to the axes. Add the text *s* to the axes at location *x*, *y* in data coordinates, using plt.text() method, where the font size can be customized by changing the font-size value.Using xticks method, get or set the current tick locations and labels of the X-axis.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) plt.xticks([1, 2, 3, 4, 5])   ... Read More

How to change fonts in matplotlib (python)?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:42:43

2K+ Views

Using plt.text() method, we can increase the font size.StepsUsing plt.plot() method, we can create a line with two lists that are passed in its argument.Add text to the axes. Add the text *s* to the axes at location *x*, *y* in data coordinates, using plt.text() method. Font size can be customized by changing the font-size value.To show the figure, use plt.show() method.Exampleimport matplotlib.pyplot as plt plt.plot([1, 2, 4], [1, 2, 4]) plt.text(2, 3, "y=x", color='red', fontsize=20) # Increase fontsize by increasing value. plt.show()Output

Python matplotlib multiple bars

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:41:20

455 Views

We can use a user-defined method, autolabel, to annotate the axis value. Before that, we can initialize the fig and ax using plt.subplots() method.StepsCreate lists, labels, men_means, and women_means with different data elements.Return evenly spaced values within a given interval, using numpy.arrange() method.Set the width variable i.e., width=0.35.Create fig and ax variables using subplots method, where default nrows and ncols are 1.The bars are positioned at *x* with the given *align*\ment. Their dimensions are given by *height* and *width*. The vertical baseline is *bottom* (default 0), so create rect1 and rect2 using plt.bar() method.Set the Y-axis label using plt.ylabel() method.Set ... Read More

Annotate bars with values on Pandas bar plots in Python

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:39:08

496 Views

In this program, we can create a data frame and can plot a bar using df.plot.bar(x='lab', y='value', color='#5fba34') plot.StepsUsing Panda’s dataframe, we can create a data frame with the given dictionary, where the keys are lab and value, values of these keys are lists, respectively.Using Pandas plot.bar() method, we can create a vertical bar plot. A bar plot is a plot that presents categorical data with rectangular bars with lengths proportional to the values that they represent.To show the figure, use the plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt df = pd.DataFrame({'lab': ['A', 'B', 'C'], ... Read More

Getting vertical gridlines to appear in line plot in matplotlib

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:37:50

4K+ Views

Using plt.grid(axis="x") method, we can plot vertical gridlines.StepsMake a list of numbers.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.Toggle the gridlines, and optionally set the properties of the lines, using plt.grid() method.To show the figure, use the plt.show() method, where the argument axis can be “x”, “y” or “both”.Examplefrom matplotlib import pyplot as plt plt.plot([0, 5], [0, 5]) plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.grid(axis="x") plt.show()Output

How to plot a high resolution graph in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:36:08

17K+ Views

We can use the resolution value, i.e., dots per inch, and the image format to plot a high-resolution graph in Matplotlib.StepsCreate a dictionary with Column 1 and Column 2 as the keys and Values are like i and i*i, where i is from 0 to 10, respectively.Create a data frame using pd.DataFrame(d); d created in step 1.Plot the data frame with ‘o’ and ‘rx’ style.To save the file in pdf format, use savefig() method where the image name is myImagePDF.pdf, format="pdf".We can set the dpi value to get a high-quality image.Using the saving() method, we can save the image with ... Read More

How do I get interactive plots again in Spyder/Ipython/matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 08:31:31

3K+ Views

To get interactive plots, we need to activate the figure. Using plt.ioff() and plt.ion(), we can perform interactive actions with plot.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Draw a line, using plot() method.Set the color of the line, i.e., orange.Stopped the interaction, using plt.ioff() method.To make the interaction plots, change the color of the line coordinate.Start the interaction, using plt.ion() method.ExampleTo use interactive plot in Ipython -In [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as plt In [3]: fig, ax = plt.subplots()    # Diagram will ... Read More

Save the plots into a PDF in matplotlib

Rishikesh Kumar Rishi
Updated on 12-Sep-2023 03:11:35

69K+ Views

Using plt.savefig("myImagePDF.pdf", format="pdf", bbox_inches="tight") method, we can save a figure in PDF format.StepsCreate a dictionary with Column 1 and Column 2 as the keys and Values are like i and i*i, where i is from 0 to 10, respectively.Create a data frame using pd.DataFrame(d), d created in step 1.Plot the data frame with ‘o’ and ‘rx’ style.To save the file in PDF format, use savefig() method where the image name is myImagePDF.pdf, format = ”pdf”.To show the image, use the plt.show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt d = {'Column 1': [i for i in ... Read More

Advertisements