Found 10476 Articles for Python

How to color a Matplotlib scatterplot using a continuous value?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:19:56

7K+ Views

To color a matplotlib scatterplot using continuous value, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z random data points using numpy.Create a figure and a set of subplots.Create a scatter plot.Draw a colorbar in an existing axes, with scatter points scalar mappable instance.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 x, y, z = np.random.rand(3, 50) f, ax = plt.subplots() points = ax.scatter(x, y, c=z, s=50, cmap="plasma") f.colorbar(points) ... Read More

Text alignment in a Matplotlib legend

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:18:42

5K+ Views

To make text alignment in a matplotlib legend, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x data points using numpy.Plot x, sin(x) and cos(x) using plot() method.Place legend using legend() method and initialize a method.Iterate the legend.get_texts() method to set the horizontal alignment.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-5, 5, 100) plt.plot(x, np.sin(x), label="$y=sin(x)$") plt.plot(x, np.cos(x), label="$y=cos(x)$") legend = plt.legend(loc='upper right') for t in ... Read More

Plot Matplotlib 3D plot_surface with contour plot projection

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:16:49

746 Views

To plot 3d plot_surface with contour plot projection, we can use plot_surface() and contourf() methods.StepsSet the figure size and adjust the padding between and around the subplots.Create x, y, X, Y and Z data points using numpy.Create a new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement, with 3D projection.Use plot_surface() method to create a surface plot.Create a 3D filled contour plotm using contourf() method.Trurn off the axes.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

How to add Matplotlib Colorbar Ticks?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:15:23

5K+ Views

To add ticks to the colorbar, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Use imshow() method to display the data as an image, i.e., on a 2D regular raster.Create ticks using numpy in the range of min and max of z.Create a colorbar for a ScalarMappable instance, *mappable*, with ticks=ticks.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 x, y = np.mgrid[-1:1:100j, -1:1:100j] z = (x + ... Read More

How to change the font properties of a Matplotlib colorbar label?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:14:11

3K+ Views

To change the font properties of a matplotlib colorbar label, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x, y and z data points using numpy.Use imshow() method to display the data as an image, i.e., on a 2D regular raster.Create a colorbar for a ScalarMappable instance, *mappable*.Using colorbar axes, set the font properties such that the label is bold.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 x, y = np.mgrid[-1:1:100j, -1:1:100j] z = ... Read More

How can I draw a scatter trend line using Matplotlib?

Rishikesh Kumar Rishi
Updated on 04-Jun-2021 06:12:46

6K+ Views

To draw a scatter trend line using matplotlib, we can use polyfit() and poly1d() methods to get the trend line points.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y data points using numpy.Find the trend line data points using polyfit() and poly1d() method.Plot x and p(x) data points using plot() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(100) y ... Read More

How to plot hexbin histogram in Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:40:05

487 Views

To plot a hexbin histogram in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y using hexbin() method.Set the title of the plot.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = 2 * np.random.randn(5000) y = x + np.random.randn(5000) fig, ax = plt.subplots() _ = ax.hexbin(x[::10], y[::10], gridsize=20, cmap='plasma') ax.set_title('Hexbin Histogram') ... Read More

How to plot a 2D histogram in Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:38:46

3K+ Views

To plot a 2D histogram in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y using hist2d() method.Set the title of the plot.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = 2 * np.random.randn(5000) y = x + np.random.randn(5000) fig, ax = plt.subplots() _ = ax.hist2d(x[::10], y[::10]) ax.set_title('2D Histogram') plt.show()OutputRead More

How to make joint bivariate distributions in Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:37:49

298 Views

To make joint bivariate distributions in matplotlib, we can use the scatter method.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Create a figure and a set of subplots.Plot x and y using scatter() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = 2 * np.random.randn(5000) y = x + np.random.randn(5000) fig, ax = plt.subplots() _ = ax.scatter(x, y, alpha=0.08, cmap="copper", c=x) plt.show()OutputRead More

How to align the bar and line in Matplotlib two Y-axes chart?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 13:36:31

2K+ Views

To align the bar and line in matplotlib two Y-axes chart, we can use twinx() method to create a twin of Axes with a shared X-axis but independent Y-axis.StepsSet the figure size and adjust the padding between and around the subplots.Make a Pandas dataframe with columns 1 and 2.Plot the dataframe using plot() method with kind="bar", i.e., class by name.Use twinx() method to create a twin of Axes with a shared X-axis but independent Y-axis.Plot the axis (Step 3) ticks and dataframe columns values to plot the lines.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import ... Read More

Advertisements