Found 10476 Articles for Python

How to create a heat map in Python that ranges from green to red? (Matplotlib)

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:03:17

1K+ Views

To create a heatmap in Python that ranges from green to red, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dictionary for different colors.Create a colormap from linear mapping segments using LinearSegmentedColormap.Create a figure and a set of subplots.Create random data with 5☓5 dimension.Create a pseudocolor plot with a non-regular rectangular grid.Create a colorbar for a ScalarMappable instance, *mappable*.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import matplotlib.colors as colors import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True cdict = {'red': ... Read More

How do I get all the bars in a Matplotlib bar chart?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:00:45

438 Views

To get all the bars in a Matplotlib chart, we can use the bar() method and return the bars.−StepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Create x and y data points using subplots() method.Make a bar plot and store it in bars variable.Set the facecolor of a particular set of bars.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 fig, ax = plt.subplots() x = np.arange(7) y = np.random.rand(7) bars = ax.bar(x, ... Read More

How to remove a frame without removing the axes tick labels from a Matplotlib figure in Python?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:59:03

2K+ Views

To remove a frame without removing the axes tick labels from a Matplotlib figure, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a list of y data points.Plot the y data points using plot() methodTo remove the left-right-top and bottom spines, we can use set_visible() method.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y = [0, 2, 1, 5, 1, 2, 0] plt.plot(y, color='red', lw=7) for pos in ['right', 'top', 'bottom', 'left']:    plt.gca().spines[pos].set_visible(False) plt.show()OutputRead More

How to get a reverse-order cumulative histogram in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:57:25

2K+ Views

To get a reverse-order cumulative histogram in Matplotlib, we can use cumulative = -1 in the hist() method.Set the figure size and adjust the padding between and around the subplots.Make a list of data points.Plot a histogram with data and cumulative = -1.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [1, 2, 2, 3, 1, 4, 3, 0, 1, 3, 0] plt.hist(data, edgecolor='black', align="mid", cumulative=-1) plt.show()Output

How to use an update function to animate a NetworkX graph in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:55:10

2K+ Views

To use an update function to animate a NetworkX graph in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Initialize a graph with edges, name, and graph attributes.Add nodes to the graph using add_nodes_from() method.Draw the graph G with Matplotlib.Use FuncAnimation() class to make an animation by repeatedly calling a function, animate.Function animate clears the current figure, generate two random numbers, and draws the edges between them.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, ... Read More

How to plot a pcolor colorbar in a different subplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:52:31

3K+ Views

To plot a pcolor colorbar in a different subplot in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots wuth two rows and two columns.Make a list of colormaps.Iterate the axes and create a pseudocolor plot with a non-regular rectangular grid.Make colorbars with the same axes of pcolormesh.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, axs = plt.subplots(2, 2) cm = ['plasma', 'copper'] for col ... Read More

How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:48:33

4K+ Views

To plot a smooth 2D color plot for z = f(x, y) 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.Get z data points using f(x, y).Display the data as an image, i.e., on a 2D regular raster, with z data points.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 def f(x, y):    return np.array([i * i + j * j for j in ... Read More

Display two Sympy plots as one Matplotlib plot (add the second plot to the first)

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 10:47:19

1K+ Views

To display two sympy plots as one Matplotlib plot, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Transform strings into instances of :class:'Symbol' class.Plot a function of a single variable as a curve.Use the extend method to add all the series of plot2 (p2) in plot1 (p1).To display the figure, use show() method.Examplefrom sympy import symbols from sympy.plotting import plot from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = symbols('x') p1 = plot(x*x, show=False) p2 = plot(x, show=False) p1.extend(p2) p1.show()OutputRead More

Deleting a Label in Python Tkinter

Dev Prakash Sharma
Updated on 19-Jun-2021 08:47:30

23K+ Views

Tkinter label widgets are used to display text and images in the application. We can also configure the properties of Label widget that are created by default in a tkinter application.If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.ExampleIn this example, we will create a Button that will allow the user to delete the label from the widget.# Import the required libraries from tkinter import * from tkinter import ttk from PIL import Image, ImageTk # Create an instance of tkinter frame or window win = ... Read More

How to hide and show canvas items on Tkinter?

Dev Prakash Sharma
Updated on 19-Jun-2021 08:34:29

4K+ Views

The Canvas widget is one of the versatile widgets in Tkinter. It is used in many applications for designing the graphical user interface such as designing, adding images, creating graphics, etc. We can add widgets in the Canvas widget itself. The widgets that lie inside the canvas are sometimes called "Canvas Items".If we want to show or hide the canvas items through a Button, then this can be achieved by using the "state" property in itemconfig(id, state) method.ExampleIn this example, we will add an image in the canvas and a button will be used to show/hide the image in the ... Read More

Advertisements