Found 1034 Articles for Matplotlib

Rotate theta=0 on a Matplotlib polar plot

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:47:22

1K+ Views

To set theta=0 on a matplotlib polar plot, we can take the following steps −Create random theta in the range of 0 to 100; convert them into radian.Using set_theta_zero_location() method, we can set the location of theta to 0.Plot theta_in_rad and data_r using plot() method.Set the title of the plot using title() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import random plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True theta_in_rad = [float(i) * np.pi / 180.0 for i in range(0, 100, 10)] data_r = random.sample(range(70, 90), 10) ax = plt.subplot(111, polar=True) ax.set_theta_zero_location("W") ax.plot(theta_in_rad, data_r, color='r', linewidth=3) ax.set_title("Example", ... Read More

What is the difference between plt.close() and plt.clf() in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:47:02

1K+ Views

plt.figure() - Create a new figure or activate an existing figure.plt.figure().close() - Close a figure window.close() by itself closes the current figureclose(h), where h is a Figure instance, closes that figureclose(num) closes figure number numclose(name), where name is a string, closes the figure with that labelclose('all') closes all the figure windowsplt.figure().clear() - It is the same as clf.plt.cla() - Clear the current axes.plt.clf() - Clear the current figure.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) y = np.linspace(1, 2, 10) plt.plot(x, y, c='red') plt.title("First Plot") plt.show() ... Read More

How to add a variable to Python plt.title?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:46:22

10K+ Views

To add a varaible to Python plt.title(), we can take the following steps −Create data points for x and y using numpy and num (is a variable) to calculate y and set this in title.Plot x and y data points using plot() method with red color.Set the title of the curve with variable num.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) num = 2 y = num ** x plt.plot(x, y, c='red') plt.title(f"y=%d$^x$" % num) plt.show()OutputRead More

How to plot data from multiple two-column text files with legends in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:46:01

2K+ Views

To plot data from multiple two-column text files with legends in matplotlib, we can take the following steps −Import genfromtxt from pylab. It has several options to read data from a text file and plot the data.Read two text files,  test.txt and test1.txt (having two columns of data), using genfromtxt and store the data in two variables, firstfiledata and secondfiledata.Plot the data using plot() method. label will be displayed as the legend.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt; from pylab import genfromtxt; plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True firstfiledata = genfromtxt("test.txt"); secondfiledata = genfromtxt("test1.txt"); plt.plot(firstfiledata[:, 0], firstfiledata[:, 1], label="test.txt ... Read More

Hide Matplotlib descriptions in Jupyter notebook

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:45:24

1K+ Views

To hide matplotlib descriptions of an instance while calling plot() method, we can take the following steps −Open Ipython instance.import numpy as npfrom matplotlib, import pyplot as pltCreate points for x, i.e., np.linspace(1, 10, 1000)Now, plot the line using plot() method.To hide the instance, use plt.plot(x); i.e., (with semi-colon)Or, use _ = plt.plot(x)ExampleIn [1]: import numpy as np In [2]: from matplotlib import pyplot as plt In [3]: x = np.linspace(1, 10, 1000) In [4]: plt.plot(x) Out[4]: [] In [5]: plt.plot(x); In [6]: _ = plt.plot(x) In [7]:OutputOut[4]: []

Plot multiple columns of Pandas DataFrame using Seaborn

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:45:03

1K+ Views

To plot multiple columns of Pandas DataFrame using Seaborn, we can take the following steps −Make a dataframe using Pandas.Plot a bar using Seaborn's barplot() method.Rotate the xticks label by 45 angle.To display the figure, use show() method.Exampleimport pandas import matplotlib.pylab as plt import seaborn as sns import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame({"X-Axis": [np.random.randint(10) for i in range(10)], "YAxis": [i for i in range(10)]}) bar_plot = sns.barplot(x='X-Axis', y='Y-Axis', data=df) plt.xticks(rotation=45) plt.show()Output

Setting different error bar colors in barplot in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:44:43

771 Views

To set different error bar colors in barplot in matplotlib, we can take the following steps −Create a figure and add a set of subplots using subplots() method.Make a barplot with data range 4, heights 2. yerr means vertical errorbars to the bar tips. The values are sizes relative to the data. Dictionary of kwargs to be passed to the errorbar method. Values of ecolor or capsize defined here take precedence over the independent kwargs.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() ax.bar(range(4), [2] * 4, yerr=range(1, 5),   ... Read More

How to display Matplotlib Y-axis range using absolute values rather than offset values?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:44:24

710 Views

To display Y-axis range using absolute values rather than offset values, we can take the following steps −Create x_data and y_data data points in the range of 100 to 1000.Create a figure and a set of subplots using subplots() method.Plot x_data and y_data using plot() method.If a parameter is not set, the corresponding property of the formatter is left unchanged using ticklabel_format() method with useOffset=False.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_date = range(100, 1000, 100) y_data = range(100, 1000, 100) fig, ax = plt.subplots() ax.plot(x_date, y_data) ax.ticklabel_format(useOffset=False) plt.show()OutputRead More

Drawing lines between two plots in Matplotlib

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:32:42

2K+ Views

To draw lines between two plots in matplotlib, we can take the following steps −Create a new figure or activate an existing figure.Add two axes (ax1 and ax2) to the figure as part of a subplot arrangement.Create random data x and y using numpy.Plot x and y data points on both the axes (ax1 and ax2) with color=red and marker=diamond.Initialize two variables, i and j to get the diffirent data points on the subplot.Make xy and mn tuple for positions to add a patch on the subplots.Add a patch that connects two points (possibly in different axes), con1 and con2.Add artists for con1 ... Read More

How to display only a left and bottom box border in Matplotlib?

Rishikesh Kumar Rishi
Updated on 08-May-2021 09:30:52

3K+ Views

To display or hide box border in matplotlib, we can use spines (value could be right, left, top or bottom) and set_visible() method to set the visibility to True or False.StepsCreate x and y data points using numpy.Create a figure and add a set of subplots using subplots() method.Plot x and y data points using plot() method, where linewidth=7 and color=red.Set visibility as True for left and bottom, and False for top and right.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 ... Read More

Advertisements