Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Rishikesh Kumar Rishi
Page 60 of 102
Manipulation on horizontal space in Matplotlib subplots
To manipulate on horizontal space in Matplotlib subplots, we can use wspace=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 wspace=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(wspace=1) ...
Read MoreHow can I pass parameters to on_key in fig.canvas.mpl_connect('key_press_event',on_key)?
To pass parameters to on_key in fig.canvas.mpl_connect('key_press_event', on_key), we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Set x and y scale of the axes.Bind the function to the event.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, ax = plt.subplots() ax.set_xlim(0, 10) ax.set_ylim(0, 10) def onkey(event): if event.key.isalpha(): if event.xdata is not None and event.ydata is not None: ax.plot(event.xdata, event.ydata, 'bo-') ...
Read MoreCustomizing annotation with Seaborn's FacetGrid
To customizing annotation with seaborn's face grid, we can take following steps −Set the figure size and adjust the padding between and around the subplots.Create a data frame with col1 and col2 columns.Multi-plot grid for plotting conditional relationships.Apply a plotting function to each facet's subset of the data.Set the title of each grids.To display the figure, use show() method.Exampleimport pandas as pd import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'col1': [3, 7, 8, 1], 'col2': ["three", "seven", "one", "zero"]}) g = sns.FacetGrid(data=df, col='col2', height=3.5) g.map(plt.hist, 'col1', ...
Read MoreManipulation on vertical space in Matplotlib subplots
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 MoreHow to convert data values into color information for Matplotlib?
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 MoreInteractive plotting with Python Matplotlib via command line
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 MoreDisplaying different images with actual size in a Matplotlib subplot
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()Output
Read MoreHow do I make bar plots automatically cycle across different colors?
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()Output
Read MoreHow to change the line color in a Seaborn linear regression jointplot?
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 MoreControlling the width of bars in Matplotlib with per-month data
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