List of Available Fonts 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

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

Change X-Axis Range with Datetimes in Matplotlib

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

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

Change 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

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

Make a Multicolored Point in Matplotlib

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

301 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

Add Percentages on Top of Bars in Seaborn Using Matplotlib

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

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

Decouple Hatch and Edge Color in Matplotlib

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

923 Views

To decouple hatch and edge color in matplotlib, we can use hatch color “o” and edge color “red”.−StepsCreate a new figure or activate existing figure.Add a subplot arrangement to the current axes.Create two lists of data points.Use bar() method with hatch and edgecolor.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax1 = fig.add_subplot(111) x = [3, 6, 1] y = [4, 6, 1] ax1.bar(x, y, color='black', edgecolor='red', hatch="o", lw=1., zorder=0) plt.show()Output

Extend Bottom Margin of a Figure in Matplotlib

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

785 Views

To fix the extension of margin at the bottom of a figure, we can take the following steps −Using Pandas dataframe, create a df with the keys, time and speed.Plot df.time and df.speed using plot() method.Tick_params() is a convenience method for changing the appearance of ticks and tick labels. rotation=90 extends the tick labels at the bottom.To fix the bottom extension, use tight_layout() method.Exampleimport numpy as np import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) plt.plot(df.time, df.speed) plt.tick_params(rotation=90) plt.show()OutputRead More

Advertisements