Found 1034 Articles for Matplotlib

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

866 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

7K+ 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

5K+ 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

2K+ 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

How do I change the range of the X-axis with datetimes in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:35:17

8K+ Views

To change the range of the X-axis with datetimes in matplotlib, we can take the following steps −Create a list of x and y, where x stores the datetime and y stores the number.Using subplots method, create a figure and add a set of subplots.Plot x and y data points using plots() method, wehere markerface color is green, marker edge color is red, and marker size is 7.Since date ticklabels often overlap, so it is useful to rorate them and right-align them using autofmt_xdate() method.To change the range of X-axis with datetimes, use set_xlim() with range of datetimes.To change the range of Y-axis, use set_ylim() method.To ... Read More

Determine Matplotlib axis size in pixels

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

3K+ Views

To determine the axis size in pixels, we can take the following steps −Create a figure and a set of subplots, using subplots() method, fig and ax.To get the DPI, use fig.dpi. Print the details.Find bounding box in the display box.Find the width and height, using bbox.width and bbox.height.Print the width and height.Examplefrom matplotlib import pyplot as plt fig, ax = plt.subplots() print("Dot per inch(DPI) for the figure is: ", fig.dpi) bbox = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) width, height = bbox.width, bbox.height print("Axis sizes are(in pixels):", width, height)OutputDot per inch(DPI) for the figure is: 100.0 Axis sizes are(in pixels): 4.96 3.696Read More

How to make a multicolored point in Matplotlib?

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

218 Views

To make a multicolored point in matplotlib, we can take the following steps−Initialize two varuables, x and y.Use scatter method with x and y data points with green color having marker size 2000.Use scatter method with x and y data points with red color having marker size 1000.Use scatter method with x and y data points with blue color having marker size 500.Use scatter method with x and y data points with white color having marker size 10.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x, y = 0, ... Read More

How to add percentages on top of bars in Seaborn using Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:33:49

6K+ Views

To add percentages on top of bars in Seaborn, we can take the following steps −Create the lists, x, y and percentages to plot using Seaborn.Using barplot, show point estimates and confidence intervals with bars. Store the returned axis.Find patches from the returned axis (In step 2).Iterate the patches (returned in step 3).Find x and y from the patches to place the percentage value at the top of the bars.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, ... Read More

Advertisements