Found 33676 Articles for Programming

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

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

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

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

297 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

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

How to decouple hatch and edge color in Matplotlib?

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

917 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

What does a 4-element tuple argument for 'bbox_to_anchor' mean in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:27:08

477 Views

If a 4-tuple or B box Base is given, then it specifies the b box (x, y, width, height) that the legend is placed in.StepsCreate x and y data points using numpy.Plot x and y using plot() method, with label y=sin(x) and color=green.To place the legend at a specific location, use location 'upper left' and use legend box dimension with four tuples that was defined in the above description.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 = np.sin(x) plt.plot(x, ... Read More

Advertisements