Plot 1D Data at a Given Y Value with Pylab using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:24:09

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()OutputRead More

Insert Degree Symbol into Python Plot using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:19:02

11K+ 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()OutputRead More

Get List of Axes for a Figure in Pyplot

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:18:35

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

Get Rid of Grid Lines When Plotting with Seaborn and Pandas

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:17:02

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

Save Matplotlib Figure with Legend Outside the Plot

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:16:34

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

Change the Scale of an Existing Table in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:16:13

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 Black and White Binary Map in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:13:17

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

Create a Legend for a Contour Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:12:49

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

Change Text Color of Font in Legend using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:12:25

6K+ Views

To change the text color of font in the legend in matplotlib, we can take the following steps−Create x and y data points using numpy.Plot x and y using plot() method, where color of line is red and label is "y=exp(x)".To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text.To set the color of the text, use set_color() method with green color.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 = ... Read More

Overlapping Y-Axis and X-Axis Tick Labels in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:11:58

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

Advertisements