Found 10476 Articles for Python

How to set axis ticks in multiples of pi in Python Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:44:15

5K+ Views

To set axis ticks in multiples of pi in Python, we take following steps −Initialize a pi variable, create theta and y data points using numpy.Plot theta and y using plot() method.Get or set the current tick locations and labels of the X-axis using xticks() method.Convenience method to set or retrieve autoscaling margins using margins() 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 pi = np.pi theta = np.arange(-2 * pi, 2 * pi+pi/2, step=(pi / 2)) y = np.sin(theta) plt.plot(theta, y) plt.xticks(theta, ['-2π', '-3π/2', 'π', ... Read More

How to make hollow square marks with Matplotlib in Python?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:40:16

6K+ Views

To make hollow square marks with matplotlib, we can use marker 'ks', markerfacecolor='none', markersize=15, and markeredgecolor=red.StepsCreat x and y data points using numpy.Create a figure or activate an existing figure, add an axes to the figure as part of a subplot arrangement.Plot x and y data points using plot() method. To make hollow square marks, we can use marker "ks" and markerfacecolor="none", markersize="15" and markeredge color="red".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) fig = plt.figure() ax1 = ... Read More

How to display all label values in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:37:08

7K+ Views

To display all label values, we can use set_xticklabels() and set_yticklabels() methods.StepsCreate a list of numbers (x) that can be used to tick the axes.Get the axis using subplot() that helps to add a subplot to the current figure.Set the ticks on X and Y axes using set_xticks and set_yticks methods respectively and list x (from step 1).Set tick labels with label lists (["one", "two", "three", "four"]) and rotation of 45 using set_xticklabels() and set_yticklabels().To add space between axes and tick labels, we can use tick_params() method with pad argument that helps to add space. Argument direction (in) helps to put ticks inside ... Read More

How can I place a table on a plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:31:31

809 Views

StepsUsing the subplots() method, create a figure and a set of subplots with figure size (7, 7).Create a data frame with two keys, time and speed.Get the size of the array.Add a table to the current axis using the table method.Shrink the font size until the text fits into the cell width.Set the font size in the table.Set the face color, edge color,  and text color by iterating the matplotlib table.Save and display the figure.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 fig, ax = plt.subplots() df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), ... Read More

Draw axis lines or the origin for Matplotlib contour plot.

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:22:57

5K+ Views

To draw axis lines or the origin for matplotlib contour plot, we can use contourf(),  axhline() y=0 and axvline() x=0.Create data points for x, y, and z using numpy.To set the axes properties, we can use plt.axis('off') method.Use contourf() method with x, y, and z data points.Plot x=0 and y=0 lines with red color.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1.0, 1.0, 10) x, y = np.meshgrid(x, x) z = -np.hypot(x, y) plt.axis('off') plt.contourf(x, y, z, 10) plt.axhline(0, color='red') plt.axvline(0, color='red') plt.show()OutputRead More

How to plot a line graph from histogram data in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:19:44

8K+ Views

To plot a line graph from histogram data in matplotlib, we use numpy histogram method to compute the histogram of a set of data.StepsAdd a subplot to the current figure, nrows=2, ncols=1 and index=1.Use numpy histogram method to get the histogram of a set of data.Plot the histogram using hist() method with edgecolor=black.At index 2, use the computed data (from numpy histogram). To plot them, we can use plot() 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 plt.subplot(211) data = np.array(np.random.rand(100)) y, binEdges = np.histogram(data, bins=100) plt.hist(data, bins=100, edgecolor='black') ... Read More

How to plot a multi-colored line, like a rainbow using Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:15:51

2K+ Views

To plot multi-colored lines, like a rainbow, we can create a list of seven rainbow colors (VIBGYOR).StepsCreate x for data points using numpy.Create a list of colors (rainbow VIBGYOR).Iterate in the range of colors list length.Plot lines with x and y(x+i/20) using plot() method, with marker=o, linewidth=7 and colors[i] where i is the index.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, 10) colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] for i in range(len(colors)):    plt.plot(x, x+i/20, c=colors[i], lw=7, marker='o') plt.show()OutputRead More

How to remove the label on the left side in matplotlib.pyplot pie charts?

Rishikesh Kumar Rishi
Updated on 11-May-2021 12:11:13

3K+ Views

To remove the label on the left side in a matplotlib pie chart, we can take the following steps −Create lists of hours, activities, and colors.Plot a pie chart using pie() method.To hide the label on the left side in matplotlib, we can use plt.ylabel("") with ablank string.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True hours = [8, 1, 11, 4] activities = ['sleeping', 'exercise', 'studying', 'working'] colors = ["grey", "green", "orange", "blue"] plt.pie(hours, labels=activities, colors=colors, autopct="%.2f") plt.ylabel("") plt.show()Output

How to animate a line plot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 28-May-2021 15:16:11

3K+ Views

To animate the line plot in matplotlib, we can take the following steps −Create a figure and a set of subplots using subplots() method.Limit x and y axes scale.Create x and t data points using numpy.Return coordinate matrices from coordinate vectors, X2 and T2.Plot a line with x and F data points using plot() method.To make animation plot, update y data.Make an animation by repeatedly calling a function *func*, current fig, animate, and interval.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = ... Read More

How can I display text over columns in a bar chart in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 11:59:47

3K+ Views

To display text over columns in a bar chart, we can use text() method so that we could place text at a specific location (x and y) of the bars column.StepsCreate lists for x, y and percentage.Make a bar plot using bar() method.Iterate zipped x, y and percentage to place text for the bars column.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 = ['A', 'B', 'C', 'D', 'E'] y = [1, 3, 2, 0, 4] percentage = [10, 30, 20, 0, 40] ax = plt.bar(x, y) for x, y, p in zip(x, y, percentage): ... Read More

Advertisements