Found 26504 Articles for Server Side Programming

Adding caption below X-axis for a scatter plot using Matplotlib

Rishikesh Kumar Rishi
Updated on 06-May-2021 13:00:19

5K+ Views

To add caption below X-axis for a scatter plot, we can use text() method for the current figure.StepsCreate x and y data points using numpy.Create a new figure or activate an existing figure using figure() method.Plot the scatter points with x and y data points.To add caption to the figure, use text() method.Adjust the padding between and around the subplots.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 x = np.random.rand(10) y = np.random.rand(10) fig = plt.figure() plt.scatter(x, y, c=y) fig.text(.5, .0001, "Scatter Plot", ha='center') plt.tight_layout() plt.show()OutputRead More

How to draw a rectangle over a specific region in a Matplotlib graph?

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

749 Views

To draw a rectangle over a specific region in a matplotlib graph, we can take the following steps −Using subplots() method, create a figure and a set of subplots, where nrows=1.Using rectangle, we can create a rectangle, defined via an anchor point and its width and height. Where, edgecolor=orange, linewidth=7, and facecolor=green.To plot a diagram over the axis, we can create a line using plot() method, where line color is red.Add a rectangle patch on the diagram, using add_patch() method.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True figure, ax = plt.subplots(1) ... Read More

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

432 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

970 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

Advertisements