Programming Articles - Page 1203 of 3363

Set Max value for color bar on Seaborn heatmap using Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:52:19

681 Views

To set a value for color bar on Seaborn heatmap, we can take following Steps−Create random data using numpy.Use heatmap() method to plot rectangular data as a color-encoded matrix.To display the figure, use show() method.Exampleimport numpy as np import seaborn as sns 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) ax = sns.heatmap(data, vmax=1) plt.show()Output

Putting arrowheads on vectors in Matplotlib's 3D plot

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:47:53

2K+ Views

To draw arrow heads vectors in 3D matplotlb's plot, we can take the following steps −Create a 2D array, where x, y, z, u, v and w are the coordinates of the arrow locations and direction components of arrow vectors.Using figure() method, create a new figure or activate an existing figure.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, using add_subplot() method.Plot a 3D field of arrows, using quiver() method.Using ylim, xlim, zlim, limit the range of the axes.Set the title of the plot.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = ... Read More

Extract csv file specific columns to list in Python

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:45:13

16K+ Views

To extract csv file for specific columns to list in Python, we can use Pandas read_csv() method.StepsMake a list of columns that have to be extracted.Use read_csv() method to extract the csv file into data frame.Print the exracted data.Plot the data frame using plot() method.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True columns = ["Name", "Marks"] df = pd.read_csv("input.csv", usecols=columns) print("Contents in csv file:", df) plt.plot(df.Name, df.Marks) plt.show()The csv file contains the following data −NameMarksArun98Shyam75Govind54Javed92Raju87OutputWhen we execute the code, it will extract the data from the csv file ... Read More

Remove the X-axis ticks while keeping the grids (Matplotlib)

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:46:18

3K+ Views

To remove the X-ticks while keeping the grids, we can take the following steps−Use gca() method to get the current axes, creating one if necessary.Plot the x and np.sin(x) using plot() method with linewidth=5, label y=sin(x).Remove yticks and xticks by passing empty array in the argument of set_xticklabels and set_yticklabels methods respectively.Configure grid lines by putting flag as True.Place the legend for the plot label in the argument.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2*np.pi, 100) ax = plt.gca() ax.plot(x, np.sin(x), c='r', lw=5, ... Read More

How to make a log histogram in Python?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:44:51

4K+ Views

To make a log histogram, we can use log=True in the argument of the hist() method.StepsMake a list of numbers.Plot a histogram with density=True.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True k = np.array([5, 5, 5, 5]) x, bins, p = plt.hist(np.log(k), density=True, log=True) plt.show()Output

How to shade the regions between the curves in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:44:30

6K+ Views

To shade the regions between curves, we can use the fill_between() method.StepsInitialize the variable n. Initiliize x and y data points using numpy.Create a figure and a set of subplots, fig and ax.Plot the curve using plot method.Use fill_between() method, fill the area between the two curves.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 = 256 X = np.linspace(-np.pi, np.pi, n, endpoint=True) Y = np.sin(2 * X) fig, ax = plt.subplots() ax.plot(X, Y, color='blue', alpha=1.0) ax.fill_between(X, 0, Y, color='blue', alpha=.2) plt.show()OutputRead More

Plotting a transparent histogram with non-transparent edge in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:45:49

1K+ Views

To plot a transparent histogram with non-transparent edge, we can take the following steps−Create a set of random data points (y).Initialize the number of bins to be drawn.To plot the histogram, we can use hist() method with edge color and facecolor tuplesTo 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 y = np.random.rand(100) nbins = 5 plt.hist(y, bins=nbins, edgecolor=(1, 0, 0, 1), lw=5, facecolor=(.09, .12, .65, .87), rwidth=0.8) plt.show()Output

How to share secondary Y-axis between subplots in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:42:32

4K+ Views

To share secondary Y-axis between subplots in matplotlib, we can take the following steps −Create x for data points.Add a subplot to the current figure, with nrows=2, ncols=1, at index=1 (ax0)Using twinx() method, create a twin of axes with a shared X-axis but independent Y-axis (ax1).Add a subplot to the current figure, with nrows=2, ncols=1 at index=2 (ax2)Using twinx() method, create a twin of axes with a shared X-axis but independent Y-axis (ax3).Using get_shared_y_axes() method, return a reference to the shared axes Grouper object for Yaxis.Create curves c1,  c2,  c3 and c4 with different colors, x and y data points.To move the legend box ... Read More

Line plot with arrows in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:44:09

5K+ Views

To plot with arrows in matplotlib, we can use arrow() method.StepsCreate x and y data points using numpy.Plot x and y with color=red and linewidth = 1.Use arrow method to add an arrow to the axes. The first two values in the arguments are the coordinates of the arrow base and the next two values are for the length of the arrow along X and Y direction.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.sin(x) plt.plot(x, y, c='b', ... Read More

How to center an annotation horizontally over a point in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-May-2021 07:43:18

1K+ Views

To center an annotation horizontally over a point, we can take the following steps−Create points for x and y using numpy.Create labels using xpoints.Use scatter() method to scatter the points.Iterate labels, xpoints and ypoints and annotate the plot with label, x and y with different properties, make horizontal alignment ha=center.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 xpoints = np.linspace(1, 10, 10) ypoints = np.random.rand(10) labels = ["%.2f" % i for i in xpoints] plt.scatter(xpoints, ypoints, c=xpoints) for label, x, y in zip(labels, xpoints, ypoints): ... Read More

Advertisements