Manipulation on Vertical Space in Matplotlib Subplots

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:48:05

3K+ Views

To manipulate on vertical space in Matplotlib subplots, we can use hspace=1 in subplots_adjust() method without tight plot layout.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots with 4 indices.To adjust the vertical space, we can use hspace=1.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2 * np.pi, 400) y = np.sin(x ** 2) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2) fig.subplots_adjust(hspace=1) ... Read More

Convert Data Values into Color Information for Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:46:24

1K+ Views

To convert data values into color information for Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Get a colormap instance, defaulting to rc values if *name* is None.Create random values that could be converted into color information.Create random data points, x and y.Use scatter() method to plot x and y.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plasma = plt.get_cmap('GnBu_r') values = np.random.rand(100) x = np.random.rand(len(values)) y = np.random.rand(len(values)) sc = plt.scatter(x, ... Read More

Interactive Plotting with Python Matplotlib via Command Line

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:44:34

1K+ Views

To get interactive plots, we need to activate the figure. Using plt.ioff() and plt.ion(), we can perform interactive actions with a plot.Open Ipython shell and enter the following commands on the shell.ExampleIn [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as In [3]: fig, ax = plt.subplots() # Diagram will pop up. Let’s interact. In [4]: ln, = ax.plot(range(5)) # Drawing a line In [5]: ln.set_color("orange") # Changing drawn line to orange In [6]: plt.ioff() # Stopped interaction In [7]: ln.set_color("red") # Since we have stopped the interaction ... Read More

Display Different Images with Actual Size in Matplotlib Subplot

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:42:49

3K+ Views

To display different images with actual size in a Matplotlib subplot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Read two images using imread() method (im1 and im2)Create a figure and a set of subplots.Turn off axes for both the subplots.Use imshow() method to display im1 and im2 data.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True im1 = plt.imread("bird.jpg") im2 = plt.imread("opencv-logo.png") fig, ax = plt.subplots(nrows=1, ncols=2) ax[1].axis('off') ax[1].imshow(im1, cmap='gray') ax[0].axis('off') ax[0].imshow(im2, cmap='gray') plt.show()OutputRead More

Automatically Cycle Bar Plots Across Different Colors

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:41:10

852 Views

To male bar plots automatically cycle across different colors, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Set automatic cycler for different colors.Make a Pandas dataframe to plot the bars.Use plot() method with kind="bar" to plot the bars.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rc('axes', prop_cycle=(plt.cycler('color', ['r', 'g', 'b', 'y']))) df = pd.DataFrame(dict(name=["John", "Jacks", "James"], age=[23, 20, 26], marks=[88, 90, 76], salary=[90, 89, 98])) df.set_index('name').plot(kind='bar') plt.show()OutputRead More

Change Line Color in Seaborn Linear Regression Jointplot

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:38:51

930 Views

To change the line color in seaborn linear regression jointplot, we can use joint_kws in jointplot() method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy to make a Pandas dataframe.Use jointplot() method with joint_kws in the arguments.To display the figure, use show() method.Exampleimport seaborn as sns import numpy as np from matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True X = np.random.randn(1000, ) Y = 0.2 * np.random.randn(1000) + 0.5 df = pd.DataFrame(dict(x=X, y=Y)) g = sns.jointplot(x="x", y="y", ... Read More

Controlling the Width of Bars in Matplotlib with Per Month Data

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:36:24

647 Views

To control the width of bars in matplotlib with per-month data, we can take the following steps −Set the figure size and adjust the padding between and around the subplotsMake a list of dates, x and y, using numpy.Plot the bar with x and y data points, with per-month data.To display the figure, use show() method.Exampleimport numpy as np import datetime from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = [datetime.datetime(2021, 1, 1, 0, 0),    datetime.datetime(2021, 2, 1, 0, 0),    datetime.datetime(2021, 3, 1, 0, 0),    ] y = ... Read More

Animate a Sine Curve in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:32:48

3K+ Views

To make animated sine curve, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an axes to the current figure and make it the current axes.Plot a line with empty lists.To initialize the line, pass empty lists.To animate the sine curve, update sine curve values and return the line instance.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt from matplotlib import animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ... Read More

Matplotlib Histogram with Multiple Legend Entries

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:28:05

4K+ Views

To plot a histogram with multiple legend entries, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create random data using numpyPlot a histogram using hist() method.Make a list of colors to color the face of each patch.Iterate the patches and set face color of each patch.Create a list of handles to place the legend.Use legend() method for multiple legend entries.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Rectangle plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rayleigh(size=1000) * ... Read More

Differentiate Orthographic and Perspective Projection in Matplotlib

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:25:33

608 Views

To plot perspective and orthographic projection plots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set the projection type as 'perspective' on ax1 axis.Set the title of the plot.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Set the projection type as 'orthographic' on ax2 axis.Set the title of the plot.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig ... Read More

Advertisements