Programming Articles - Page 1205 of 3363

Plot scatter points using plot method in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:47:24

636 Views

To plot scatter points using plot method in matplotlib, we can take the following steps−Create random data points (x1 and x2) using numpy.Plot x1 data points using plot() method with marker size 20 and green color.Plot x2 data points using plot() method with marker size 10 and red color.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x1 = np.random.randn(20) x2 = np.random.randn(20) plt.plot(x1, 'go', markersize=20) plt.plot(x2, 'ro', ms=10) plt.show()Output

How to create a standard colorbar for a series of plots in Python?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:44:46

452 Views

To create a standard colorbar for a series of plots, we can take the following steps −Create random data using numpy.Create a figure and a set of subplot using subplots() method, where nrows=1 and ncols=1.Display data as an image.Add an axes to the figure, for colorbar.Create a colorbar where mappable instance is image and cax where color will be drawn.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(4, 4) fig, ax = plt.subplots(nrows=1, ncols=1) im = ax.imshow(data) cax = fig.add_axes([0.9, 0.1, 0.03, 0.8]) fig.colorbar(im, cax=cax) ... Read More

Changing the formatting of a datetime axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:47:02

7K+ Views

To change the formatting of a datetime axis in matplotlib, we can take the following steps−Create a dataframe df using pandas DataFrame with time and speed as keysCreate a figure and a set of subplots using subplots() method.Plot the dataframe using plot method, with df's (Step 1) time and speed.To adjust the tick labels, we can rotate tick_params by 45 degreesTo edit the date formatting from %d-%m-%d to %d:%m%d, we can use set_major_formatter() method. Set the formatter of the major ticker.To display the figure, use show() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt, ... Read More

Save figure as file from iPython notebook using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:46:34

5K+ Views

To save a figure as a file from iPython, we can take the following steps−Create a new figure or activate an existing figure.Add an axes to the figure using add_axes() method.Plot the given list.Save the plot using savefig() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_axes([1, 1, 1, 1]) plt.plot([1, 2]) plt.savefig('test.png', bbox_inches='tight')OutputWhen we execute the code, it will save the following plot as "test.png".

How to plot 2D math vectors with Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:46:09

3K+ Views

To plot 2D math vectors with matplotlib, we can take the following steps−Create vector cordinates using numpy array.Get x, y, u and v data points.Create a new figure or activate an existing figure using figure method.Get the current axis using gca() method.Set x an y limits of the axes.To redraw the current figure, use draw() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True soa = np.array([[0, 0, 3, 2], [0, 0, 4, 5], [0, 0, 9, 9]]) X, Y, U, V = zip(*soa) plt.figure() ax = plt.gca() ... Read More

Wrapping long Y labels in Matplotlib tight layout using setp

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:45:34

1K+ Views

To wrap long Y label in matplotlib tight layput using setp, we can take the following steps−Create a list of a long strings.Create a tuple of 3 values.Create a figure and add a set of subplots.Limit the Y-axis ticks using ylim() method.Make a horizontal bar plot, using barh() method.Use yticks() method to ticks the yticks.Use setp() method to set a property on an artist object.Use tight_layout() method to adjust the padding between and around subplots.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True labels = ( ... Read More

Top label for Matplotlib colorbars

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:36:51

8K+ Views

To place a top label for colorbars, we can use colorbar's axis to set the title.StepsCreate random data using numpy.Use imshow() method to represent data into an image, with colormap "PuBuGn" and interpolation= "nearest".Create a colorbar for a scalar mappable instance, imSet the title on the ax (of colorbar) using set_title() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.randn(4, 4) im = plt.imshow(data, interpolation='nearest', cmap="PuBuGn") clb = plt.colorbar(im) clb.ax.set_title('Color Bar Title') plt.show()OutputRead More

Darken or lighten a color in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:36:30

6K+ Views

To darken and lighten the color, we can chage the alpha value in the argument of plot() method.Greater the aplha value, darker will be the color.StepsCreate data points for xs and ys using numpy.Plot two lines with different value of alpha, to replicate darker and lighter color of the linesPlace legend of the plot using legend() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xs = np.linspace(-2, 2, 100) ys = np.sin(xs) plt.plot(xs, ys, c='red', lw=10, label="Darken") plt.plot(xs+.75, ys+.75, c='red', lw=10, alpha=0.3, label="Lighten") plt.legend(loc='upper left') ... Read More

How to get a list of all the fonts currently available for Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:37:23

3K+ Views

To get a list of all the fonts currently available for matplotlib, we can use the font_manager.findSystemFonts() method.StepsPrint a statement.Use font_manager.findSystemFonts() method to get a list of fonts availabe.Examplefrom matplotlib import font_manager print("List of all fonts currently available in the matplotlib:") print(*font_manager.findSystemFonts(fontpaths=None, fontext='ttf'), sep="")Output/usr/share/fonts/truetype/Nakula/nakula.ttf /usr/share/fonts/truetype/ubuntu/Ubuntu-L.ttf /usr/share/fonts/truetype/tlwg/Loma-BoldOblique.ttf ................................................................. ............................................................................ ................................................................................. ........ /usr/share/fonts/truetype/lohit-malayalam/Lohit-Malayalam.ttf /usr/share/fonts/truetype/tlwg/TlwgTypist-Oblique.ttf /usr/share/fonts/truetype/liberation2/LiberationMono-Bold.ttfRead More

How to change the plot line color from blue to black in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:34:57

3K+ Views

To change the plot line color from blue to black, we can use setcolor() method−StepsCreate x and y data points using numpy.Plot line x and y using plot() method; store the returned value in line.Set the color as black using set_color() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 10) y = 4 * x + 5 line, = plt.plot(x, y, c='b') line.set_color('black') plt.show()Output

Advertisements