
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 10476 Articles for Python

1K+ Views
To plot and work with NaN values in matplotlib, we can take the following steps −Create data using numpy with some NaN values.Use imshow() method to display data as an image, i.e., on a 2D regular raster, with a colormap and data (from step 1).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.array([[1., 1.2, 0.89, np.NAN], [1.2, np.NAN, 1.89, 2.09], [.78, .67, np.NAN, 1.78], [np.NAN, 1.56, 1.89, 2.78]] ) plt.imshow(data, cmap="gist_rainbow_r") plt.show()Output

400 Views
To display print statements interlaced with matplotlib plots inline in iPython, we can take the following steps.StepsImport pyplot from matplotlib.Make a list of data for hist plots.Initialize a variable "i" to use in print statement.Iterate the list of data (Step 2).Create a figure and a set of subplots using subplots() method.Place print statement.Plot the histogram using hist() method.Increase "i" by 1.ExampleIn [1]: from matplotlib import pyplot as plt In [2]: myData = [[7, 8, 1], [2, 5, 2]] In [3]: i = 0 In [4]: for data in myData: ...: fig, ax = plt.subplots() ...: print("data number i =", ... Read More

2K+ Views
To remove relative shift in matplotlib axis, we can take the following steps −Plot a line with two input lists.Using gca() method, get the current axis and then return the X-axis instance. Get the formatter of the major ticker. To remove the relative shift, use set_useOffset(False) method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot([10, 101, 1001], [1, 2, 3]) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) plt.show()Output

6K+ Views
To define the size of a grid on a plot, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add an axes to the figure as a part of a subplot arrangement.Plot a curve with an input list.Make x and y margins 0.To set X-grids, we can pass input ticks points.To lay out the grid lines in current line style, use grid(True) method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) ax.plot([0, 2, 5, 8, 10, 1, 3, 14], ... Read More

815 Views
To get information for bins in matplotlib histogram function, we can take the following steps −Create a list of numbers for data and bins.Compute the histogram of a set of data using histogram() method.Get the hist and edges from the histogram (step 2).Find the frequency in a histogram.Make a bar with bins (Step 1) and freq (step 4) data.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 a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45] bins = np.arange(-180, 181, 20) hist, edges = np.histogram(a, bins) freq = hist/float(hist.sum()) plt.bar(bins[:-1], freq, width=20, ... Read More

4K+ Views
To adjust the space between legend markers and labels, we can use labelspacing in legend method.StepsPlot lines with label1, label2 and label3.Initialize a space variable to increase or decrease the space between legend markers and label.Use legend method with labelspacing in the arguments.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot([0, 1], [0, 1.0], label='Label 1') plt.plot([0, 1], [0, 1.1], label='Label 2') plt.plot([0, 1], [0, 1.2], label='Label 3') space = 2 plt.legend(labelspacing=space) plt.show()Output

3K+ Views
To redefine a color for a specific value in matplotlib colormap, we can take the following steps −Get a colormap instance, defaulting to rc values if *name* is None using get_cmap() method, with gray colormap.Set the color for low out-of-range values when "norm.clip = False" using set_under() method.Using imshow() method, display data an image, i.e., on a 2D regular raster.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True cmap = cm.get_cmap('gray') cmap.set_under('red') plt.imshow(np.arange(25).reshape(5, 5), interpolation='none', cmap=cmap, vmin=.001) plt.show()OutputRead More

77K+ Views
To set X-axis values in matplotlib in Python, we can take the following steps −Create two lists for x and y data points.Get the xticks range value.Plot a line using plot() method with xtick range value and y data points.Replace xticks with X-axis value using xticks() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [45, 1, 34, 78, 100] y = [8, 10, 23, 78, 2] default_x_ticks = range(len(x)) plt.plot(default_x_ticks, y) plt.xticks(default_x_ticks, x) plt.show()Output

2K+ Views
To make a rotating 3D graph in matplotlib, we can use Animation class for calling a function repeatedly.StepsInitialize variables for number of mesh grids, frequency per second to call a function, frame numbers.Create x, y, and z array for a curve.Make a function to make z array using lambda function.To pass a function into the animation class, make a user-defined function to remove the previous plot and plot a surface using x, y, and zarray.Create a new figure or activate an existing figure.Add a subplot arrangement using subplots() method.Set the Z-axis limit using set_zlim() method.Call the animation class to animate the surface plot.To display ... Read More

11K+ Views
To plot two graphs side-by-side in Seaborn, we can take the following steps −To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7).Create a data frame with keys, col1 and col2, using Pandas.Use countplot() to show the counts of observations in each categorical bin using bars.Adjust the padding between and around the subplots.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np import seaborn as sns from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True f, axes = plt.subplots(1, 2) df = pd.DataFrame(dict(col1=np.linspace(1, 10, 5), col2=np.linspace(1, 10, 5))) sns.countplot(df.col1, x='col1', ... Read More