Found 1034 Articles for Matplotlib

How to place X-axis grid over a spectrogram in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:13:58

369 Views

To place X-axis grid over a spectrogram in Python, we can use grid() method and take the following steps −Set the figure size and adjust the padding between and around the subplots.Create t, s1, s2, nse, x, NEFT and Fs data points using numpy.Create a new figure or activate an existing figure using subplots() method with nrows=2.Plot t and x data points using plot() method.Lay out a grid in current line style.Set X-axis margins.Plot a spectrogram using specgram() method.Lay out a grid in current line style with dotted linestyle and some other properties.To display the figure, use show() method.Exampleimport matplotlib.pyplot ... Read More

How to hide axes but keep axis-labels in 3D Plot with Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:11:21

2K+ Views

To hide axes but keep axis-labels in 3D plot with Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Create x, y, z, dx, dy and dz data points using numpyUse bar3d() method to plot the 3D bars.To hide the axes, initialize a color tuple, the same as axes color.Set x, y and z axes plane color property same with the color tuple.Set x, y and z axes line color property ... Read More

How to plot a rectangle on a datetime axis using Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:09:23

1K+ Views

To plot a recatangle on a datetime axis using matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement using add_subplot() method.To define a rectangle, find the anchor points using datetime and matplotlib's dates.Add a '~.Patch' to the axes' using add_patch() method.Set major axis locator and formatter.Limit x and y axes scale.To display the figure, use show() method.Examplefrom datetime import datetime, timedelta from matplotlib.patches import Rectangle import matplotlib.pyplot as plt ... Read More

How to remove scientific notation from a Matplotlib log-log plot?

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

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

How to turn off the 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

How to work with images in Bokeh (Python)?

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

609 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

How to show legend elements horizontally in Matplotlib?

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

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

How to change the color of a single bar if a condition is true (Matplotlib)?

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

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

How to annotate each cell of a heatmap in Seaborn?

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

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

How to plot multiple Pandas columns on the Y-axis of a line graph (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

Advertisements