Customize Color and Colormaps of a Plot in Matplotlib

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

617 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

Plot Time Zones on a Map in Matplotlib

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

509 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

Yen's K-Shortest Path Algorithm in Data Structure

Dev Prakash Sharma
Updated on 23-Feb-2021 06:35:29

2K+ Views

Instead of giving a single shortest path, Yen’s k-shortest path algorithm gives k shortest paths so that we can get the second shortest path and the third shortest path and so on.Let us consider a scenario that we have to travel from place A to place B and there are multiple routes available between place A and place B, but we have to find the shortest path and neglect all the paths that are less considered in terms of its time complexity in order to reach the destination.Let us understand with an example-Consider the given example as the bridge which ... Read More

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

Provide Shadow Effect in a Plot Using Path Effect Attribute in Matplotlib

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

1K+ 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

Plot with Multiple Color Cycle Using Cycler Property in Matplotlib

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

842 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

Plot Exponentially Decaying Function Using FuncAnimation in Matplotlib

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

290 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

Create Custom Markers on a Plot in Matplotlib

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

860 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

Significance of regex.match() and regex.search() Function in Python

Dev Prakash Sharma
Updated on 23-Feb-2021 05:50:24

630 Views

There are two types of operations that can be performed using regex, (a) search and (b) match. In order to use regex efficiently while finding the pattern and matching with the pattern, we can use these two functions.Let us consider that we have a string. regex match() checks the pattern only at the beginning of the string, while regex search() checks the pattern anywhere in the string. The match() function returns the match object if a pattern is found, otherwise none.match() – Finds the pattern only at the beginning of the string and returns the matched object.search() – Checks for ... Read More

Breadth-First Search on Matrix in Python

Dev Prakash Sharma
Updated on 23-Feb-2021 05:37:17

2K+ Views

In a given matrix, there are four objects to analyze the element position: left, right, bottom, and top.Breadth First Search is nothing but finding the shortest distance between the two elements of a given 2-D Matrix. Thus, in each cell, there are four operations we can perform which can be expressed in four numerals such as, '2' describes that the cell in the matrix is Source.'3' describes that the cell in the matrix is Destination.'1' describes that the cell can be moved further in a direction.'0' describes that the cell in the matrix can not be moved in any direction.On ... Read More

Advertisements