Remove Scientific Notation from Matplotlib Log-Log Plot

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:06:49

4K+ Views

To remove scientific notation from a matplotlib log-log plot, we can use ax.xaxis.set_minor_formatter(mticker.ScalarFormatter()) statement.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using scatter() method.Set x and y axes sacle using set_xscale() and set_yscale() methods.To remove scientific notation, use format tick values as a number.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, ticker as mticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 7, 6, 4, 0]) y = np.array([6, 2, 3, ... Read More

Turn Off Upper Right Axis Tick Marks in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:50:54

1K+ Views

To turn off the upper or right axis ticks marks in matplotlib, we can make a custom dictionary visible_ticks and turn off the flag.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using plot() method.Make a dictionary to turn off the axis ticks marks.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 x = np.linspace(-2, 2, 10) y = np.sin(x) plt.plot(x, y) visible_ticks = { "top": False, ... Read More

Work with Images in Bokeh Python

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:49:14

790 Views

To work with images in Bokeh, use image_url() method and pass a list of images.StepsConfigure the default output state to generate output saved to a file when :func:'show' is called.Create a new Figure for plotting.Render the images loaded from the given URLs.Immediately display a Bokeh object or application.Examplefrom bokeh.plotting import figure, show, output_file output_file('image.html') p = figure(x_range=(0, 1), y_range=(0, 1)) p.image_url(url=['bird.jpg'], x=0, y=1, w=0.8, h=0.6) show(p)Output

Show Legend Elements Horizontally in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:47:03

7K+ Views

To show legend elements horizontally, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Using plot() method, plot lines with the labels line1, line2 and line3.Place a legend on the figure using legend() method, with number of labels for ncol value in the argument.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3], label="line1") line2, = plt.plot([3, 2, 1], label="line2") line3, = plt.plot([2, 3, 1], label="line3") plt.legend(ncol=3, loc="upper right") plt.show()OutputRead More

Change Color of a Single Bar in Matplotlib Based on Condition

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:45:08

6K+ Views

To change the color of a single bar if a condition is true, we can make a set of values and a list of colors with red until the value is 2; else add yellow color in the list.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable width of a bar.Make two lists of values and colors.Use bar() method to plot bars.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 data = np.arange(5) width = 0.5 vals = [1, 2, 1, 5, ... Read More

Annotate Each Cell of a Heatmap in Seaborn

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:43:16

2K+ Views

To annotate each cell of a heatmap, we can make annot = True in heatmap() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with 5 columns.Use sns.heatmap() to plot a dataframe (Step 2) with annot=True flag in the argument.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns.heatmap(df, annot=True, annot_kws={"size": 7}) plt.show()OutputRead More

Plot Multiple Pandas Columns on Y-Axis of Line Graph using Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:41:47

1K+ Views

To plot multiple Pandas columns on the Y-axis of a line graph, we can set the index using set_index() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with Category 1, Category 2, and Category 3 columns.Use set_index() method to set the DataFrame index using existing columns.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'Category 1': [2, 4, 5, 1, 0, 3], 'Category 2': [6, 3, 1, 4, 5, 2], 'Category 3': [2, 4, 1, 3, 6, 0]}) df.set_index('Category 1').plot() plt.show()Output

Differences Between AGG and Cairo Backends in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:37:49

625 Views

RendererFile typesDescriptionAGGPngRaster graphics − high-quality images using the Anti-Grain Geometry engineCairopng, ps, pdf, svgRaster or vector graphics − using the Cairo libraryStepsSet the figure size and adjust the padding between and around the subplots.Set the backend name as "Agg".Create a 5☓5 matrix array using numpy.Use imshow() method to display data as an image, i.e., on a 2D regular raster.To save the figure, use savefig() method.Exampleimport matplotlib as mpl import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True mpl.use("Agg") data = np.random.rand(5, 5) plt.imshow(data, interpolation='nearest', cmap="copper") plt.savefig('agg.png')OutputRead More

Find the Area Between Two Curves in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:35:20

1K+ Views

To find the area between two curves plot in matplotlib, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create x, c1 and c2 data points using numpy.Plot (x, c1) and (x, c2) using plot() methods.Fill the area between the two curves, c1 and c2, with grey color and hatch "|", using fill_between() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 1, 100) c1 = x ** 2 c2 = x plt.plot(x, c1) plt.plot(x, c2) plt.fill_between(x, ... Read More

Make the Parula Colormap in Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:34:01

1K+ Views

To make the Parula colormap in matplotlib, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create colormap data using numpy.Create a 'LinearSegmentedColormap' from a list of colors.Viscum is a little tool for analyzing colormaps and creating new colormaps.Use imshow() method to display data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Examplefrom matplotlib.colors import LinearSegmentedColormap import matplotlib.pyplot as plt import numpy as np from viscm import viscm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True cm_data = np.random.rand(4, 4) parula_map = LinearSegmentedColormap.from_list('parula', cm_data) viscm(parula_map) plt.imshow(np.linspace(0, 100, ... Read More

Advertisements