Found 33676 Articles for Programming

How to create a draggable legend in Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:52:08

2K+ Views

To create a draggable legend in matplotlib, we can take the following steps −Create two lines, line1 and line2, using plot() method.Place the legend for plot line1 and line2 with ordered lables at location 1, using legend() method.To create a draggable legend, use set_draggable() method, where state=True. If state=False, then we can't drag the legend.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True line1, = plt.plot([1, 2, 3]) line2, = plt.plot([3, 2, 1]) leg = plt.legend([line2, line1], ["line 2", "line 1"], loc=1) leg.set_draggable(state=True) plt.show()OutputOn the output window, you can drag the legend around with ... Read More

Automatically Rescale ylim and xlim in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:51:49

2K+ Views

To rescale ylim and xlim automatically, we can take the following steps −To plot a line, use plot() method and data range from 0 to 10.To scale the xlim and ylim automatically, we can make the variable scale_factore=6.Use scale_factor (from Step 2) to rescale the xlim and ylim, using xlim() and ylim() methods, respectively.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.plot(range(0, 10)) scale_factor = 6 xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() plt.xlim(xmin * scale_factor, xmax * scale_factor) plt.ylim(ymin * scale_factor, ymax * scale_factor) plt.show()OutputRead More

How do I plot Shapely polygons and objects using Matplotlib?

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:51:12

6K+ Views

To plot shapely polygons and objects using matplotlib, the steps are as follows −Create a polygon object using (x, y) data points.Get x and y, the exterior data, and the array using polygon.exterior.xy.Plot x and y data points using plot() method with red color.Examplefrom shapely.geometry import Polygon import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True polygon1 = Polygon([(0, 5),    (1, 1),    (3, 0),    (4, 6), ]) x, y = polygon1.exterior.xy plt.plot(x, y, c="red") plt.show()Output

Set variable point size in Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 12:53:39

433 Views

To set the variable point size in matplotlib, we can take the following steps−Initialize the coordinates of the point.Make a variable to store the point size.Plot the point using scatter method, with marker=o, color=red, s=point_size.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True xy = (3, 4) point_size = 100 plt.scatter(x=xy[0], y=xy[1], marker='o', c='red', s=point_size) plt.show()Output

What is the difference between a variable and StringVar() of Tkinter?

Dev Prakash Sharma
Updated on 04-May-2021 14:17:08

978 Views

A variable in Tkinter is used to store the values of any data. For a Tkinter application, we can store the values in two ways −by defining the value programmatically, orby storing the value through user Input.A normal variable can be used to set the value for any application whenever it is required. However, we can take the user input by creating an instance of the StringVar() object. When we specify a Tkinter variable such as textvariable, for a widget (textvariable = myvar), the widget automatically gets updated whenever the value of the variable changes. However, there might be times ... Read More

How to control automated window resizing in Tkinter?

Dev Prakash Sharma
Updated on 04-May-2021 14:17:33

5K+ Views

The Tkinter window can be resized manually by defining the geometry ("width × height") method. We can automate or reset the window to its original form by passing an empty value to the geometry manager. Once the empty value is passed to the method, it will get resized automatically. In this example, we will create a Tkinter application that will display a Toplevel window (Popup window) with a defined size. When we press a RESET button, it will be resized again to its default size.Example#Import the library from tkinter import * from tkinter import ttk #Create an instance of ... Read More

Tkinter button commands with lambda in Python

Dev Prakash Sharma
Updated on 03-May-2021 11:50:48

31K+ Views

Lamda Functions (also referred to as Anonymous Function in Python) are very useful in building Tkinter GUI applications. They allow us to send multiple data through the callback function. Lambda can be inside any function that works as an anonymous function for expressions. In Button Command, lambda is used to pass the data to a callback function.ExampleIn this example, we will create an application that will have some buttons in it. The button command is defined with the lambda function to pass the specific value to a callback function.#Import the library from tkinter import * from tkinter import ttk ... Read More

Taking input from the user in Tkinter

Dev Prakash Sharma
Updated on 14-Sep-2023 13:13:52

41K+ Views

There might be times when we need to take the user input in our Tkinter application. We can get the user Input in a Single Line text input through the Entry widget using get() method. To display the Captured input, we can either print the message on the screen or display the input with the help of the Label widget.Example#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win= Tk() #Set the geometry of Tkinter frame win.geometry("750x250") def display_text():    global entry    string= entry.get()    label.configure(text=string) ... Read More

Set style for Labelframe in Python Tkinter

Dev Prakash Sharma
Updated on 03-May-2021 11:46:40

3K+ Views

Tkinter LabelFrame is similar to Frames in Tkinter Library. It works like a container where widgets can be placed. LabelFrame initially creates a container with some rectangular border around it. In order to style the LabelFrame widget, we have several style options such as background, borderwidth, labelanchor, highlightcolor and many more.ExampleIn this example, we will see the LabelFrame widget and its properties.#Import required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Define the geometry of the window win.geometry("750x250") #Initialize a LabelFrame Widget labelframe= LabelFrame(win, text= "Frame 01", width= 600, height= 200, labelanchor= "n", font= ('Helvetica ... Read More

Rock Paper and Scissor Game Using Tkinter

Dev Prakash Sharma
Updated on 03-May-2021 11:44:59

3K+ Views

Tkinter is one of the Python-based libraries used to create and develop Desktop User interfaces and applications. Using the Tkinter library and its packages, we will create a Rock Paper Scissor Game Application. The game can be played between two people using hand gestures. The condition for winning the game is, If player A gets Paper and Player B gets scissors, then Scissor wins.If player A gets Paper and Player B gets Rock, then Paper wins.Similarly, If player A gets Rock and Player B gets scissors, then Rock wins.Following these game conditions, we will first create the GUI for the game user interface. The ... Read More

Advertisements