Fill Polygon with Custom Hatch in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:25:24

1K+ Views

To fill a polygon with a custom hatch in matplotlib, we can override the matplotlib.hatch.Shapes class.StepsSet the figure size and adjust the padding between and around the subplots.Make a hatch shape using polygon class and get the path.Override the custom hatch shape class, with shape_vertices, shape_codes, etc.Create a new figure or activate an existing figure.Add an axes to the figure as part of a subplot arrangement.Add an axes patch of shape polygon.Set the hatching pattern.To display the figure, use show() method.Exampleimport matplotlib.hatch import matplotlib.pyplot as plt from matplotlib.patches import Polygon plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True my_hatch_shape ... Read More

Create Curved Edges with NetworkX in Python3 Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:21:22

2K+ Views

To create curved edges with NetworkX in Python3, we can use connectionstyle="arc3, rad=0.4".StepsSet the figure size and adjust the padding between and around the subplots.Initialize a graph with edges, name, and graph attributes.Add nodes to the created graph.Add edges from one node to another.Draw the graph G with Matplotlib, with connectionstyle="arc3, rad=0.4".To display the figure, use show() method.Exampleimport matplotlib.pylab as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True G = nx.DiGraph() pos = nx.spring_layout(G) G.add_nodes_from([1, 2, 3, 4]) G.add_edges_from([(1, 2), (2, 4), (2, 3), (4, 1)]) nx.draw(G, with_labels=True, connectionstyle="arc3, rad=0.4") plt.show()OutputRead More

Create Diverging Stacked Bar Chart in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:20:10

1K+ Views

To create a diverging stacked bar chart in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Initialize a variable N to get the number of indices.Get menMeans, womenMeans, menStd and womenStd tuple.Initialize the width of bars.Create a figure and a set of subplots.To get diverging bar, we can put the data with positive and negative values to make diverging bars.Add a horizontal line across the axis.Set Ylabel, title, ticks, ticklabels, and legend.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, ... Read More

Plot PySpark SQL Results Using Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:18:13

4K+ Views

To use Matplotlib to plot PySpark SQL results, we can take the following steps−Set the figure size and adjust the padding between and around the subplots.Get the instance that is the main Entry Point for Spark functionality.Get the instance of a variant of Spark SQL that integrates with the data stored in Hive.Make a list of records as a tuple.Distribute a local Python collection to form an RDD.Map the list record as a DB schema.Get the schema instance to make an entry into "my_table".Insert a record into a table.Read the SQL query, retrieve the record.Convert the fetched record into a ... Read More

Manipulate Figures While Script is Running in Python Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:16:02

599 Views

To manipulate figures while a script is running in Python, 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.Get the current axis, ax, and show the current figure.Manipulate the script using plt.pause() method, before the final plot.Plot the line 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 fig = plt.figure() ax = fig.gca() fig.show() for i in range(20):   ... Read More

Plot Kernel Density Plot of Dates in Pandas using Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:13:59

871 Views

To plot a kernel density plot of dates in Pandas using Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe.Format the Pandas date column.Plot the Pandas date as kernel density estimate class by name.Set xtick labels using set_xticklabels() method.To display the figure, use show() method.Exampleimport pandas as pd import numpy as np import datetime import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True dates = pd.date_range('2010-01-01', periods=31, freq='D') df = pd.DataFrame(np.random.choice(dates, 100), columns=['dates']) df['ordinal'] = [x.toordinal() for x in df.dates] ... Read More

Get Length of a Single Unit on an Axis in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:11:15

502 Views

To get the length of a single unit on an axis 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 new figure or activate an existing figure using figure() method.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Plot x and y data points using plot() method.To get the single unit length, use transData transform.Print the horizontal and vertical lengths.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] ... Read More

Redraw an Image Using Python's Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:09:33

1K+ Views

To redraw an image using python's 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.Get the current axis using gca() method.Show the current figure.Iterate in the range of 20 and redraw the plot.Use plot() method to plot random data points.Redraw on the figure and pause for a while.Close a figure window.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.gca() fig.show() for i in range(20): ... Read More

Coloring Intersection of Circles Patches in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:08:11

904 Views

To color the intersection of circles/patches in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a and b points.Get the left, right and middle area from the two points, a and b.Get the current axes using gca() methodAdd patches with different colors and sections.Set the X and Y axes scale.Set the aspect ratios equal.Turn off the axes.To display the figure, use show() method.Exampleimport shapely.geometry as sg import matplotlib.pyplot as plt import descartes plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = sg.Point(-.5, 0).buffer(1.) b = sg.Point(0.5, ... Read More

Change DPI of Pandas DataFrame Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 07-Jul-2021 11:06:49

4K+ Views

To change the DPI of a Pandas DataFrame plot, we can use rcParams to set the dot per inch.StepsSet the figure size and adjust the padding between and around the subplots.Set the DPI values in .rcParams["figure.dpi"] = 120Create a Pandas dataframe to make a plot.Plot the dataframe.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams["figure.dpi"] = 120 data = pd.DataFrame({"column1": [4, 6, 7, 1, 8]}) data.plot() plt.show()Output

Advertisements