
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

5K+ Views
Let's take an example. We create a set of data points such that it would generate some warnings. We will create data points x from −1 to 1 and try to find log in that range, which means it will throw an error at value 0, while calculating logs.StepsCreate data points for x and calculate log(x), using numpy.Plot x and y using plot() method.Use warnings.filterwarnings("ignore") to suppress the warning.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import warnings plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True warnings.filterwarnings("ignore") x = np.linspace(-1, 1, 10) y ... Read More

8K+ Views
To insert a small image on the corner of a plot with matplotlib, we can take the following steps−Read an image from a file into an array using imread() method.Using subplots() method, create a figure and add a set of subplots.Plot a line on the current axis.Create newax (new axis) to show the image array (Step 1).Turn off the newly created axis, created for an image insert.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread('bird.jpg') # insert local path of the image. fig, ax = plt.subplots() ax.plot(range(10)) newax = ... Read More

3K+ Views
To draw a rectangle with only border in matplotlib, we can take the following steps−Create a figure and a set of subplots.Get the current axes, creating one if necessary.Add a patch, i.e., a rectangle to the current axes that is returned in step 2. Set the facecolor attribute to 'none'.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True figure, _ = plt.subplots() ax = plt.gca() ax.add_patch(patches.Rectangle((.25, .25), .50, .50, edgecolor='orange', facecolor='none', linewidth=2)) plt.show()Output

1K+ Views
To change the color of the ticks in the colorbar in matplotlib, we can take the following steps−Create a random 2D−Array using numpy, with 4☓4 dimension.Use imshow() method to display the data as an image.Create a colorbar using colorbar() method with scalar mappable instance of imshow().Use getp() method to return the value of an object's property or print all of them.Set the property of an artist object.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) im = plt.imshow(data, cmap="twilight_shifted_r") cbar = plt.colorbar(im) ... Read More

2K+ Views
To show an axes subplot in Python, we can use show() method. When multiple figures are created, then those images are displayed using show() method.StepsCreate x and y data points using numpy.Plot x and y using plot() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(10) y = np.exp(x) plt.plot(x, y) plt.show()Output

509 Views
To place edge color and hatch of a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a circle instance using Circle() class with an edgecolor, hatch and linewidth of the edge.Add a circle path on the plot.To place the text in the circle, use text() method.Scale X and Y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) circle = matplotlib.patches.Circle((0, 0), radius=1, edgecolor="orange", ... Read More

3K+ Views
To fill color above the curve, we can take the following steps −StepsInitialize the variable n. Initialize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot() method.Using fill_between() method, fill the area between two curves, with 1 value.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True n = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.00) ax.fill_between(X, Y, 1, color='blue', alpha=.1) plt.show()OutputRead More

1K+ Views
To create a stacked bar chart, we can use Seaborn's barplot() method, i.e., show point estimates and confidence intervals with bars.Create df using Pandas Data Frame.Using barplot() method, create bar_plot1 and bar_plot2 with color as red and green, and label as count and select.To enable legend, use legend() method, at the upper-right location.To display the figuree, use show() method.Exampleimport pandas import matplotlib.pylab as plt import seaborn as sns plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pandas.DataFrame(dict( number=[2, 5, 1, 6, 3], count=[56, 21, 34, 36, 12], select=[29, 13, 17, 21, 8] )) bar_plot1 = sns.barplot(x='number', y='count', data=df, label="count", color="red") bar_plot2 = ... Read More

2K+ Views
To plot a histogram with colors taken from colormap, we can use the setp() method.StepsCreate data points using numpy.Plot data (Step 1) using hist() method, with bins=25, rwidth=.75, ...etc.Returned values n, bins and patches can help to find col.Get a colormap instance for name "RdYlBu".Zip the col and patches.Now, using setp() method, set the property of each patch.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.random(1000) n, bins, patches = plt.hist(data, bins=25, density=True, color='red', rwidth=0.75) col = (n-n.min())/(n.max()-n.min()) cm = plt.cm.get_cmap('RdYlBu') for c, p in zip(col, ... Read More

3K+ Views
We can use the following steps to convert a figure into a numpy array −Read a figure from a directory; convert it into numpy array.Use imshow() method to display the image.Use show() method to display it.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread("bird.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.show()OutputWhen we execute the code, it will show "bird.jpg" in a plot and show its numpy array on the console.Numpy array of the image is: [[[162 162 170] [162 162 170] [160 163 170] ... [ 97 98 92] [ 98 100 95] [ 94 96 91]] [[159 159 167] [159 159 167] [157 160 167] ... [ 94 95 89] [ 95 97 92] [ 92 94 89]] [[157 158 163] [157 158 163] [154 157 164] ... [ 93 94 89] [ 95 95 93] [ 95 95 93]] ... [[163 163 165] [163 163 165] [164 164 164] ... [187 165 151] [158 131 112] [133 105 84]] [[163 163 165] [163 163 165] [163 163 163] ... [160 134 117] [143 112 92] [127 96 75]] [[164 164 166] [163 163 165] [163 163 163] ... [145 116 98] [129 98 78] [124 92 71]]]