Found 784 Articles for Data Visualization

How I can get a Cartesian coordinate system in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:34:21

623 Views

To plot a Cartesian coordinate system in matplotlib, we can take the following Steps −Initialize a variable (N) with a value.Create random data points for x and y.Plot the points using scatter method with x and y data points.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 = 50 x = np.random.rand(N) y = np.random.rand(N) plt.scatter(x, y) plt.show()Output

Bold font weight for LaTeX axes label in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:37:05

2K+ Views

To make bold font weight LaTeX axes label in matplotlib, we can take the following steps−Create x and y data points using numpy.Using subplot() method, add a subplot to the current figure.Set x and y ticks with data points x and y using set_xticks and set_yticks methods, respectively.Plot x and y using plot() method with color=red.To set bold font weight, we can use LaTeX representation.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, font_manager as fm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams["font.fantasy"] = "Comic Sans MS" x = np.array([1, 2, 3, ... Read More

How do I plot multiple X or Y axes in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:34:01

9K+ Views

To plot multiple X or Y axis, we can use twinx() or twiny() methods, we can take the following Steps −Using subplots() method, create a figure and a set of subplots.Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales.Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2.Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.Using tight_layout() method, adjust the padding between and around the subplots.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 fig, ... Read More

Getting empty tick labels before showing a plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:33:22

692 Views

To get empty tick labels before showing a plot in matplotlib, we can take the following Steps −Create a list of data points.Add a subplot to the current figure using subplot() method.Set ticks and ticklabels using set_xticks() method and set_xticklabels() method.To get the empty tick labels, use get_xticklabels(which='minor').To display the method, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4] ax1 = plt.subplot() ax1.set_xticks(x) ax1.set_xticklabels(["one", "two", "three", "four"]) print("Empty tick labels: ", ax1.get_xticklabels(which='minor')) plt.show()OutputRead More

Is it possible to plot implicit equations using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:35:16

2K+ Views

Matplotlib does not support the functionality to plot implicit equations, however, you can try a code like the one we have shown here.StepsCreate xrange and yrange data points using numpy.Return coordinate matrices from coordinate vectors using meshgrid() method.Create an equation from x and y.Create a 3D contour using contour() method with x, y and the equation.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True delta = 0.025 xrange = np.arange(-5.0, 20.0, delta) yrange = np.arange(-5.0, 20.0, delta) x, y = np.meshgrid(xrange, yrange) equation = np.sin(x) - ... Read More

Gaussian filtering an image with NaN in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:32:23

505 Views

Gaussian filtering an image with NaN values makes all the values of a matrix NaN, which produces an NaN valued matrix.StepsCreate a figure and a set of subplots.Create a matrix with NaN value in that matrix.Display the data as an image, i.e., on a 2D regular raster, data.Apply Gaussian filter on the data.Display the data as an image, i.e., on a 2D regular raster, gaussian_filter_data.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt from scipy.ndimage import gaussian_filter plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, axes = plt.subplots(2) data = np.array([[1., 1.2, 0.89, ... Read More

Setting the aspect ratio of a 3D plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:34:51

2K+ Views

To set the aspect ratio of a 3D plot in matplotlib, we can take the following steps−Using figure() method, create a new figure or activate an existing figure.Get the current axes, creating one if necessary, with projection='3d'.Create data points, R, Y and z, using numpy.Create a surface plot using R, Y and z.Set the aspect ratio using set_aspect('auto').Save the figure using savefig() method.Examplefrom matplotlib import pyplot as plt from matplotlib import cm import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.gca(projection='3d') R, Y = np.meshgrid(np.arange(0, 100, 1), np.arange(0, 60, 1)) z = ... Read More

How to plot data into imshow() with custom colormap in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:29:55

2K+ Views

To plot data into imshow() with custom colormap in matplotlib, we can take the following steps−Set the figure size and adjust the padding between and around the subplots.Create random data points using numpy.Generate a colormap object from a list of colors.Display the data as an image, i.e., on a 2D regular rasterTo display the figure, use show() method.Examplefrom matplotlib import pyplot as plt from matplotlib.colors import ListedColormap import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.random.rand(5, 5) cmap = ListedColormap(['r', 'g', 'b']) plt.imshow(data, cmap=cmap) plt.show()OutputRead More

How to turn off error bars in Seaborn Bar Plot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:29:29

622 Views

To turn off error bars in a Seaborn bar plot, we can take the following steps−Load an example dataset from the online repository (requires Internet).Show the point estimates and confidence intervals with bars.To display the figure, use show() method.Exampleimport seaborn as sns import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = sns.load_dataset('titanic') sns.barplot(x='class', y='age', hue='survived', data=df, ci=None) plt.show()Output

How to set the number of ticks in plt.colorbar in Matplotlib?

Rishikesh Kumar Rishi
Updated on 15-May-2021 12:30:17

4K+ Views

To set the number of ticks in a colorbar, we can take the following steps−Create random data using numpyDisplay the data as an image, i.e., on a 2D regular raster.Make a colorbar using colorbar() method with an image scalar mappable object.Set the ticks and tick labels of the colorbar using set_ticks() and set_ticklabels() methods.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="copper") cbar = plt.colorbar(im) cbar.set_ticks([0.2, 0.4, 0.6, 0.8]) cbar.set_ticklabels(["A", "B", "C", "D"]) plt.show()OutputRead More

Advertisements