Found 1034 Articles for Matplotlib

How to move the Y-axis ticks from the left side of the plot to the right side in matplotlib?

Rishikesh Kumar Rishi
Updated on 15-Mar-2021 07:28:01

5K+ Views

To shift the Y-axis ticks from left to right, we can perform the following steps −Create a figure using the figure() method.Using the above figure method, create the axis of the plot, using add_subplot(xyz), where x is row, y is column, and z is index.To shift the Y-axis ticks from left to right, use ax.yaxis.tick_right() where ax is axis created using add_subplot(xyz) method.Now plot the line using plot() method, with given x and y points, where x and y points can be created using np.array() method.Set up x and y labels, e.g., X-axis and Y-axis , using xlabel and ylabel ... Read More

Plotting regression and residual plot in Matplotlib

Dev Prakash Sharma
Updated on 23-Feb-2021 18:14:13

1K+ Views

To establish a simple relationship between the observations of a given joint distribution of a variable, we can create the plot for the regression model using Seaborn.To fit the dataset using the regression model, we have to first import the necessary libraries in Python.We will create plots for each regression model, (a) Linear Regression, (b) Polynomial Regression, and (c) Logistic Regression.In this example, we will use the wine quality dataset which can be accessed from here, https://archive.ics.uci.edu/ml/datasets/wine+qualityExampleimport matplotlib.pyplot as plt import seaborn as sns from scipy.stats import pearsonr sns.set(style="dark", color_codes=True) #import the dataset wine_quality = pd.read_csv('winequality-red.csv', delimiter=';') #Plotting ... Read More

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

876 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

599 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

265 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

162 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

201 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

456 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

762 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

Advertisements