Found 26504 Articles for Server Side Programming

How to 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

How to plot Time Zones in 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

How to 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

How to Plot Cluster using Clustermaps class in Matplotlib

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

285 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

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

How to customize spines of Matplotlib figures?

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

941 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

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

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

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

975 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

How to add annotations in Matplotlib Plots?

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

436 Views

To specify the details of a plot, we use annotations. To create annotations in Matplotlib Plots, we can use the ‘annotate’ method.Exampleimport matplotlib.pyplot as plt import numpy as np #Let us create a plot and use annotation at the point (5,3), x = np.arange(0,4*np.pi,0.1) plt.plot(np.sin(x), 'b-*') a = plt.annotate("(3,0)", xy=(3, 0), xycoords='data', xytext=(4.0,0.5), textcoords='data', arrowprops=dict(arrowstyle="->", color="green", lw=5, connectionstyle=("arc3,rad=0."))) plt.setp(a, size=25) #Display the plot plt.show()Output

How to Capture a pick event and use it to activate or deactivate Line Plots in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 14:52:11

538 Views

After enabling the pick event property of artists in Matplotlib, the task is to use the pick event to enable and disable the line plots for a given axis in a set of plots.In order to pick a specific line plot, we use Legend.We will use a Binary classification plot to create the ROC Curve. ROC curve or Receiver Operating Characteristics curve is used for diagnostics, weather prediction and other applications. It contains True Negative Rate (TPR) and False Positive Rate (FPR). Using ROC, we will create multiple plots of the curve.Let us Import the libraries first. Here ‘nbAgg’ is used ... Read More

Advertisements