Found 664 Articles for Machine Learning

How to manage image resolution of a graph in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 06:25:45

1K+ Views

An Image contains a 2-D matrix RGB data points which can be defined by the dots point per inch [ DPI ] of the image. The resolution of the image is important because a hi-resolution image will have much more clarity.We have a method ‘plt.savefig()’ in Matplotlib which determines the size of the image in terms of its pixels. Ideally it is having an ‘dpi’ parameter.Let’s see how we can manage the resolution of a graph in Matplotlib.Exampleimport matplotlib.pyplot as plt import numpy as np #Prepare the data for histogram np.random.seed(1961) nd = np.random.normal(13, 5, 1000) #Define the ... Read More

How to provide shadow effect in a Plot using path_effect attribute in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 06:25:32

893 Views

In order to provide path effects like shadow effect in a plot or a graph, we can use the path_effect attribute.For example, let’s see how we can use the path_effect attribute in Matplotlib add a shadow effect to a sigmoid function.import matplotlib.pyplot as plt import numpy as np from matplotlib.patheffects import PathPatchEffect, SimpleLineShadow, NormalNow let us define the size of the figure and plot the sigmoid function, plt.style.use('seaborn-deep') plt.subplots(figsize=(10, 10))Let us define the datapoints for the plot, x = np.linspace(-10, 10, 50) y = 1+ np.exp(-x))Let us define the shadow property in the plot, plt.plot(x, y, linewidth=8, color='blue', path_effects= [SimpleLineShadow(), ... Read More

How to plot with multiple color cycle using cycler property in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 06:21:53

606 Views

Matplotlib has a default color cycle for all the graphs and plots, however, in order to draw plots with multiple color cycles, we can use the cycler property of Matplotlib. It is used to plot repetitive patterns for the axis.First, we will use the Object Oriented APIs such as pyplot to plot the specific visualization.from cycler import cycler import numpy as np from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.figure import Figure from IPython.core.display import displayIn this example, we will create two objects which will repeat the cycle after every four objects. Thus, after creating two objects, the last two ... Read More

How to plot Time Zones in a map in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 14:46:09

275 Views

Let us consider that we have some data in which we have to deal with the actual time. To plot the time-zones on the map, we can use the ‘cartopy’ or ‘metPy’ package in Python. However, we can install ‘cartopy’ package in the Anaconda environment using the commands, conda install -c conda-forge cartopyOrconda install -c conda-forge metpyNow, let’s see how to plot time zones in a map using Matplotlib.Exampleimport numpy as np import cartopy.crs as ccrs import matplotlib.animation as animation import matplotlib.pyplot as plt #Defining the plot size and axes plt.figure(figsize=(10, 9)) ax = plt.axes(projection=ccrs.PlateCarree()) #Apply the color ... Read More

How to plot Exponentially Decaying Function using FuncAnimation in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 06:17:50

171 Views

Let us assume that we want to animate a nature of function which is exponentially decaying like y = a(b)^x where b = growth factor and a = initial value.An exponentially decay function would look like this, However, for now, we want to animate and plot the exponentially decaying tan function.First import the libraries, import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimationDefine the axes, fig, a = plt.subplots()Plotting a blank figure with axes,   xdata, ydata = [], [] line, = ax.plot(xdata, ydata)Set the limit of the grids, ax.set_xlim(0, 10) ax.set_ylim(-3.0, 3.0) ax.grid()   Define the function to ... Read More

How to Plot Cluster using Clustermaps class in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 19:45:42

205 Views

Let us suppose you have given a dataset with various variables and data points thus in order to plot the cluster map for the given data points we can use Clustermaps class.In this example, we will import the wine quality dataset from the https://archive.ics.uci.edu/ml/datasets/wine+quality.import matplotlib.pyplot as plt import numpy as np import seaborn as sns sns.set(style='white') #Import the dataset wine_quality = pd.read_csv(‘winequality-red.csv’ delimeter=‘;’)Let us suppose we have raw data of wine Quality datasets and associated correlation matrix data.Now let us plot the clustermap of the data, row_colors = wine_quality["quality"].map(dict(zip(wine_quality["quality"].unique(), "rbg"))) g = sns.clustermap(wine_quality.drop('quality', axis=1), standard_scale=1, robust=True, row_colors=row_colors, cmap='viridis')Plot the ... Read More

How to customize the color and colormaps of a Plot in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 14:47:58

462 Views

To customize the color and colormaps of a plot, we can use the colormap property from the color library. There are two types of colormap we can create: (a) discrete colormap and (b) continuous colormap.We will first see how to create a discrete colormap followed by continuous colormap.In the example, we will use ‘iris’ dataset to create three plots such that the first plot uses the default colormap and the other two uses RGB map to create a mixed colored plot. However, we can create as many color maps as we have clusters.Exampleimport matplotlib.pyplot as plt import pandas as pd ... Read More

How to customize spines of Matplotlib figures?

Dev Prakash Sharma
Updated on 23-Feb-2021 14:48:37

775 Views

When we plot a figure in Matplotlib, it creates four spines around the figure, top, left, bottom and right. Spines are nothing but a box surrounded with the pictorial representation of the grid which displays some ticks and tickable axes on left(y) and bottom(x).Let us see how to customize the spines in a given figure. We will create six figures to see and customize the spines for it.First import the required libraries for the workbook.import numpy as np import matplotlib.pyplot as pltLet us draw graph for sines, theta = np.linspace(0, 2*np.pi, 128) y = np.sin(theta) fig = plt.figure(figsize=(8, 6))Define the ... Read More

How to create custom markers on a plot in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 06:08:50

597 Views

To create a custom marker on a plot or graph, we use a list where we write the markers we want to see in the plot. The markers are nothing but symbols, emoji, character or any character which we want to see on the figure.In order to create the marker, we will first import the required libraries.import matplotlib.pyplot as plt import numpy as npFor now, we will create a marker on a sine curve. Let us create the grid with size (12, 6), x = np.arange(1, 2.6, 0.1) y = np.sin(2 * np.pi * x) plt.subplots(figsize=(12, 6))Here we will create ... Read More

How to align multiple plots in a grid using GridSpec Class in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 18:22:08

738 Views

Aligning the multiple plots in a grid can be very messy and it can create multiple issues like higher width and height or minimal width to align all the plots. In order to align all the plots in a grid, we use GridSpec class.Let us suppose that we have a bar graph plot and we want to align the symmetry of the plot in this sample.First Import all the necessary libraries and plot some graphs in two grids. Then, we will plot a constant error bar and a symmetric and asymmetric error bar on the first grid. In the second ... Read More

Advertisements