Found 1034 Articles for Matplotlib

How do I make sans serif superscript or subscript text in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:38:43

1K+ Views

To make superscript or subscript text in matplotlib, use LaTeX representation.StepsCreate x and y data points using numpy.Plot x and y data point using plot() method.Put the title with LateX representation using title() method.Use xlabel and ylabel methods to set the label of the axes.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 = 2**x plt.plot(x, y) plt.title('$Y=2^{X}$') plt.xlabel('$X_{data}$') plt.ylabel('$Y_{data}$') plt.show()Output

Change the spacing of dashes in a dashed line in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:37:54

1K+ Views

To change the spacing of dashes in a dashed line in matplotlib, we can take the following steps −Create data points x and y using numpy.Initialize two variables space and dash_len with value 3.Plot x and y using plot() method, with line style '--', dashes tuple stores the property of dashed line.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(-1, 1, 100) y = np.sin(x) space = 3 dash_len = 3 plt.plot(x, y, c='red', linestyle='--', dashes=(dash_len, space), lw=5) plt.show()OutputRead More

How to set the ticks on a Fixed Position in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:38:47

5K+ Views

To set the ticks on a fixed position in matplotlib, we can take the following steps −Create a figure and add a set of subplots.To set the ticks on a fixed position, create two lists with some values.Use set_yticks and set_xticks methods to set the ticks on the axes.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() xtick_loc = [0.20, 0.75, 0.30] ytick_loc = [0.12, 0.80, 0.76] ax.set_xticks(xtick_loc) ax.set_yticks(ytick_loc) plt.show()Output

How do I plot a step function with Matplotlib in Python?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:35:38

1K+ Views

To plot a step function with matplotlib in Python, we can take the following steps −Create data points for x and y.Make a step plot using step() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 3, 4, 5, 7]) y = np.array([1, 9, 16, 25, 49]) plt.step(x, y, 'r*') plt.show()Output

How can I rotate xtick labels through 90 degrees in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:34:56

7K+ Views

To rotate xtick labels through 90 degrees, we can take the following steps −Make a list (x) of numbers.Add a subplot to the current figure.Set ticks on X-axis.Set xtick labels and use rotate=90 as the arguments in the method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_xticklabels(["one", "two", "three", "four"], rotation=90) plt.show()Output

How can I make a simple 3D line with Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:33:32

199 Views

To make a simple 3D line with matplotlib, we can take the following steps −Create a new figure or activate an existing figure.Add axes to the figure as part of a subplot arrangement.Create data points for theta, z, r, x and y using numpy.Plot x, y and z using plot() method.Place a legend on the figure 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 fig = plt.figure() ax = fig.add_subplot(projection='3d') theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r ... Read More

How to plot multiple histograms on same plot with Seaborn using Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:31:28

4K+ Views

To plot multiple histograms on same plot with Seaborn, we can take the following steps −Create two lists (x and y).Create a figure and add a set of two subplots.Iterate a list consisting of x and y.Plot a histogram with histplot() method using the data in the list (step 3).Limit the X-axis range from 0 to 10.To display the figure, use show() method.Exampleimport seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 5, 1, 4, 2] y = [7, 5, 6, 4, 5] fig, ax = plt.subplots() for a in [x, y]: ... Read More

How to change the color of a plot frame in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 08:30:42

4K+ Views

To change the color of a plot frame, we can set axes ticklines and spine value into a specific color.StepsCreate a figure and add a set of subplots, using subplots method with value 4.Zip colors with axes and iterate them together.In the iteration, set the color for spines values and ticklines (x, y).Adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, (ax1, ax2, ax3, ax4) = plt.subplots(4) for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'red', 'yellow', 'blue']):    plt.setp(ax.spines.values(), color=color)    ax.plot([8, 3], ... Read More

Annotate Time Series plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:16:15

916 Views

To annotate time series plot in matplotlib, we can take the following steps −Create lists for time and numbers.Using subplots() method, create a figure and a set of subplots.Using plot_date() method, plot the data that contains dates with linestyle "-.".Annotate a point in the plot using annotate() method.Date ticklabels often overlap, so it is useful to rotate them and right-align them.To display the figure, use show() method.Exampleimport datetime as dt from matplotlib import pyplot as plt, dates as mdates plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [dt.datetime(2021, 1, 1), dt.datetime(2021, 1, 2),    dt.datetime(2021, 1, 3), dt.datetime(2021, 1, 4)] y = ... Read More

How does one insert statistical annotations (stars or p-values) into Matplotlib plots?

Rishikesh Kumar Rishi
Updated on 07-May-2021 08:16:37

237 Views

To insert statistical annotation, we can take the following steps −Create lists (x and y) of numbers.Using subplots() method, create a figure and a set of subplots.Using plot() method, plot the data that contains dates with linestyle "-.".Annotate a point in the plot using annotate() method, mean of x and y.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(-1, 1, 5) y = np.linspace(-2, 2, 5) mean_x = np.mean(x) mean_y = np.mean(y) fig, ax = plt.subplots() ax.plot(x, y, linestyle='-.') ax.annotate('*', (mean_y, mean_y), xytext=(-.50, 1), arrowprops=dict(arrowstyle='-|>')) ... Read More

Advertisements