Found 33676 Articles for Programming

How to 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

How to specify values on Y-axis in Python Matplotlib?

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

12K+ Views

To specify values on Y-axis in Python, we can take the following steps−Create x and y data points using numpy.To specify the value of axes, create a list of characters.Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively.Plot the line using x and y, color=red, using plot() method.Make x and y margin 0.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.array([0, 2, 4, 6]) y = np.array([1, 3, 5, 7]) ticks = ... Read More

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

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

How to use 'extent' in matplotlib.pyplot.imshow?

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:51:59

3K+ Views

To use extent in matplotlib imshow(), we can use extent [left, right, bottom, top].StepsCreate random data using numpy.Display the data as an image, i.e., on a 2D regular raster with data and extent [−1, 1, −1, 1] arguments.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 data = np.random.rand(4, 4) plt.imshow(data, extent=[-1, 1, -1, 1]) plt.show()Output

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

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

Matplotlib savefig with a 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

How to 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

Overlapping Y-axis tick label and X-axis tick label 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

Styling a part of label in legend in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:10:46

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

Plot a black-and-white binary map in Matplotlib

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

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

Advertisements