Articles on Trending Technologies

Technical articles with clear explanations and examples

How to plot 1D data at a given Y-value with PyLab using Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 4K+ Views

To plot 1D data at a given Y-value with pyplot, we can take the following steps−Initialize y value.Create x and y data points using numpy. zeros_like helps to return an array of zeros with the same shape and type as a given array and add y-value for y data points.Plot x and y with linestyle=dotted, color=red, and linewidth=5.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 y_value = 1 x = np.arange(10) y = np.zeros_like(x) + y_value plt.plot(x, y, ls='dotted', c='red', lw=5) plt.show()Output

Read More

Matplotlib – How to insert a degree symbol into a Python plot?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 12K+ Views

To insert a degree symbol into a plot, we can use LaTeX representation.StepsCreate data points for pV, nR and T using numpy.Plot pV and T using plot() method.Set xlabel for pV using xlabel() method.Set the label for temperature with degree symbol using ylabel() 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 pV = np.array([3, 5, 1, 7, 10, 9, 4, 2]) nR = np.array([31, 15, 11, 51, 12, 71, 41, 13]) T = np.divide(pV, nR) plt.plot(pV, T, c="red") plt.xlabel("Pressure x Volume") plt.ylabel("Temperature ($^\circ$C)") plt.show()Output

Read More

How to get the list of axes for a figure in Pyplot?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 6K+ Views

To get a list of axes of a figure, we will first create a figure and then, use get_axes() method to get the axes and set the labels of those axes.Create xs and ys using numpy and fig using figure() method. Create a new figure, or activate an existing figure.Use add_subplot() method. Add an '~.axes.Axes' to the figure as part of a subplot arrangement, where nrows=1, ncols=1 and index=1.. Get the axes of the fig, and set the xlabel and ylabel.Plot x and y data points with red color.To display the figure, use show() method.Exampleimport numpy as np from matplotlib ...

Read More

How to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 5K+ Views

To get rid of grid lines when plotting with Pandas with secondary_y, we can take the following steps −Create a data frame using DataFrame wth keys column1 and column2.Use data frame data to plot the data frame. To get rid of gridlines, use grid=False.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = pd.DataFrame({"column1": [4, 6, 7, 1, 8], "column2": [1, 5, 7, 8, 1]}) data.plot(secondary_y=[5], grid=False) plt.show()Output

Read More

Matplotlib savefig with a legend outside the plot

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 3K+ Views

To save a file with legend outside the plot, we can take the following steps −Create x data points using numpy.Plot y=sin(x) curve using plot() method, with color=red, marker="v" and label y=sin(x).Plot y=cos(x), curve using plot() method, with color=green, marker="x" and label y=cos(x).To place the legend outside the plot, use bbox_to_anchor(.45, 1.15) and location="upper center".To save the figure, use savefig() 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, 100) plt.plot(x, np.sin(x), c="red", marker="v", label="y=sin(x)") plt.plot(x, np.cos(x), c="green", marker="x", label="y=cos(x)") plt.legend(bbox_to_anchor=(.45, 1.15), loc="upper center") plt.savefig("legend_outside.png")OutputWhen we execute this code, it will ...

Read More

How to change the scale of an existing table in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 3K+ Views

To change scale of a table, we can use the scale() method. Steps −Create a figure and a set of subplots, nrows=1 and ncols=1.Create a random data using numpy.Make columns value.Make the axis tight and off.Initialize a variable fontsize to change the fontsize.To set the fontsize of the table and to scale the table, we can use 1.5 and 1.5.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, axs = plt.subplots(1, 1) data = np.random.random((10, 3)) columns = ("Column I", "Column II", "Column III") axs.axis('tight') ...

Read More

Plot a black-and-white binary map in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 6K+ Views

To plot black-and-white binary map in matplotlib, we can create and add two subplots to the current figure using subplot() method, where nrows=1 and ncols=2. To display the data as a binary map, we can use greys colormap in imshow() method.StepsCreate data using numpyAdd two sublots, nrows=1 and ncols=2. Consider index 1.To show colored image, use imshow() method.Add title to the colored map.Add a second subplot at index 2.To show the binary map, use show() method with Greys colormap.To adjust the padding between and around the subplots, use tight_layout() method.To display the figure, use show() method.Exampleimport numpy as np from ...

Read More

How do you create a legend for a contour plot in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 3K+ Views

To create a legend for a contour plot in matplotlib, we can take the following steps−Create x, y and z data points to plot the contour function.To create a 3D filled contour plot, we can use contourf() method with x, y, z and different levels.Make a list of rectangle with the returned contour signature collection and set face colorNow, place the legend in the plot using proxy (of step 3) and different labels.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, y = np.meshgrid(np.arange(10), np.arange(10)) ...

Read More

Overlapping Y-axis tick label and X-axis tick label in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 4K+ Views

To reduce the chances of overlapping between x and y tick labels in matplotlib, we can take the following steps −Create x and y data points using numpy.Add a subplot to the current figure at index 1 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Overlapping".Add a subplot to the current figure at index 2 (nrows=1 and ncols=2).Set x and y margins to 0.Plot x and y data points and add a title to this subplot, i.e., "Non Overlapping".The objective of MaxNLocator and prune ="lower" is that the smallest tick will be removed.To display the figure, ...

Read More

Styling a part of label in legend in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 06-May-2021 268 Views

To style a part of label in legend, we can take the following steps −Create data point for x using numpy.Plot a sine curve using np.sin(x) with a text label.Plot a cosine curve using np.cos(x) with a text label.To place the legend on the plot, use 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 x = np.linspace(-1, 1, 10) plt.plot(x, np.sin(x), label="This is $\it{a\ sine\ curve}$") plt.plot(x, np.cos(x), label="This is $\bf{a\ cosine\ curve}$") plt.legend(loc='lower right') plt.show()Output

Read More
Showing 31631–31640 of 61,248 articles
Advertisements