Found 10476 Articles for Python

How to draw the largest polygon from a set of points in matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:49:14

556 Views

To draw the largest polygon from a set of points in matplotlib, we can take the following steps −Import "Polygon" from matplotlib.patches.Set the figure size and adjust the padding between and around the subplots.Create a list of data points for the largest polygon.Get the polygon instance.Create a figure and a set of subplots.Add a polygon instance patch.Set the x and y scale limit.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Polygon plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True y = np.array([[1, 1], [0.5, 1.5], [2, 1], [1, 2], [2, ... Read More

How to change the face color of a plot using Matplotlib?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:32:03

7K+ Views

To change the face color of a plot using 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 the x and y data points using plot() method with color=yellow and linewidth=7.Set the facecolor of the axes, using set_facecolor().To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x ... Read More

Plotting a masked surface plot using Python, Numpy and Matplotlib

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:23:51

682 Views

To plot a masked surface plot using Python, Numpy and 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.Add an 'ax' to the figure as part of a subplot arrangement.Return the coordinate matrices from coordinate vectors, pi and theta.Create x, y and z with masked data points.Create a surface plot with x, y, and z data points.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 fig = ... Read More

Matplotlib – Drawing lattices and graphs with Networkx

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:19:25

1K+ Views

To draw lattices and graphs with networkx, we can take the following steps −Import networkx and pyplot.Set the figure size and adjust the padding between and around the subplots.Use nx.grid_2d_graph(3, 3) to get a two-dimensional grid graph. The grid graph has each node connected to its four nearest neighbors.Draw the graph G with Matplotlib.To display the figure, use show() method.Example# Import networkx and pyplot import networkx as nx from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Draw the graph G = nx.grid_2d_graph(3, 3) nx.draw(G, node_size=100) plt.show()OutputIt ... Read More

How to set the border color of the dots in matplotlib's scatterplots?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:07:59

5K+ Views

To set the border color of the dots in matplotlib scatterplots, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable "N" to store the number of sample data.Create x and y data points using numpy.Plot the x and y data points using scatter() method. To set the border color of the dots, use the edgecolors parameter in the scatter() method. Here, we have used "red" as the border color of the dots by using edgecolors='red'.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot ... Read More

Python - Add a prefix to column names in a Pandas DataFrame

AmitDiwan
Updated on 21-Sep-2021 06:16:21

318 Views

To add a prefix to all the column names, use the add_prefix() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Add a prefix to _column to every column using add_prefix() −dataFrame.add_prefix('column_') ExampleFollowing is the code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": ... Read More

How to determine the order of bars in a matplotlib bar chart?

Rishikesh Kumar Rishi
Updated on 20-Sep-2021 13:01:06

6K+ Views

To determine the order of bars in a bar chart in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Make a dataframe, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.Add a subplot to the current figure.Make a bar plot with dataframe, df.Add a subplot to the current figure.Create another dataframe, df_sorted, by column marks.Make a bar plot with df_sorted.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 df = pd.DataFrame( ... Read More

Matplotlib – Date manipulation to show the year tick every 12 months

Rishikesh Kumar Rishi
Updated on 21-Sep-2021 11:34:04

3K+ Views

To make matplotlib date manipulation so that the year tick shows up every 12 months, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create d, y, s, years, months, monthsFmt and yearsFmt using Pandas, Numpy and matplotlib dates.Use "%B" in DateFormatter to show full month names.Ue "%Y" in DateFormatter to show years.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Plot "dts" and "s" data points using plot() method.Set minor or major axes locator and formatter. Set minor_locator as months ... Read More

Python - Reverse the column order of the Pandas DataFrame

AmitDiwan
Updated on 20-Sep-2021 12:55:41

753 Views

To reverse the column order, use the dataframe.columns and set as -1 −dataFrame[dataFrame.columns[::-1]At first, import the required library −import pandas as pd Create a DataFrame with 4 columns −dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000], "Units_Sold": [ 100, 120, 150, 110, 200, 250] })Reverse the column order −df = dataFrame[dataFrame.columns[::-1]] ExampleFollowing is the code −import pandas as pd # creating dataframe dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], "Reg_Price": [7000, 1500, 5000, 8000, 9000, ... Read More

Python - Remove a column with all null values in Pandas

AmitDiwan
Updated on 20-Sep-2021 12:45:19

1K+ Views

To remove a column with all null values, use the dropna() method and set the “how” parameter to “all” −how='all'At first, let us import the required libraries with their respective aliases −import pandas as pd import numpy as npCreate a DataFrame. We have set the NaN values using the Numpy np.infdataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": [np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN] } )To remove a column with all null values, use dropna() and set the required parameters −dataFrame.dropna(how='all', axis=1, inplace=True) ExampleFollowing is the code ... Read More

Advertisements