Found 784 Articles for Data Visualization

Place text inside a circle in Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:47:57

2K+ Views

To place text inside a circle in matplotlib, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot method to the current axis.Create a Circle instance using Circle() class.Add a circle path on the plot.To place the text in the circle, we can use text() method.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) circle = matplotlib.patches.Circle((0, 0), radius=1, color='yellow') ax.add_patch(circle) plt.text(-.25, 0, "This is a Circle") plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.axis('equal') ... Read More

How to reshape a networkx graph in Python?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:45:40

266 Views

To reshape a networkx graph in Python, we can take the following steps −Create a data frame using Panda's data frame.Return a graph from Pandas DataFrame containing an edge list using from_pandas_edgelist() method.Draw the graph G with matplotlib. We can reshape the network by increasing and decreasing the list of keys "from" and "to".To display the figure, use show() method.Exampleimport pandas as pd import networkx as nx from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'from': ['A', 'B', 'C', 'A'], 'to': ['D', 'A', 'E', 'C']}) G = nx.from_pandas_edgelist(df, 'from', 'to') nx.draw(G, with_labels=True, node_size=150, alpha=0.5, linewidths=40) plt.show()OutputRead More

Plot a circle inside a rectangle in Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:44:02

995 Views

To place a circle inside a rectangle, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add a subplot to the current axis.Create a rectangle and a circle instance.Add a rectangle patch to the current axis.Add a circle patch to the current axis.Scale x and y axes using xlim() and ylim() methods.To display the figure, use show() method.Exampleimport matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect = patches.Rectangle((2, 2), 8, 5, color='yellow') circle = patches.Circle((6, 4.5), radius=2, color='red') ax.add_patch(rect) ax.add_patch(circle) plt.xlim([-10, 10]) ... Read More

How to plot and work with NaN values in Matplotlib?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:42:04

768 Views

To plot and work with NaN values in matplotlib, we can take the following steps −Create data using numpy with some NaN values.Use imshow() method to display data as an image, i.e., on a 2D regular raster, with a colormap and data (from step 1).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 data = np.array([[1., 1.2, 0.89, np.NAN],    [1.2, np.NAN, 1.89, 2.09],    [.78, .67, np.NAN, 1.78],    [np.NAN, 1.56, 1.89, 2.78]] ) plt.imshow(data, cmap="gist_rainbow_r") plt.show()Output

How to display print statements interlaced with Matplotlib plots inline in iPython?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:40:28

296 Views

To display print statements interlaced with matplotlib plots inline in iPython, we can take the following steps.StepsImport pyplot from matplotlib.Make a list of data for hist plots.Initialize a variable "i" to use in print statement.Iterate the list of data (Step 2).Create a figure and a set of subplots using subplots() method.Place print statement.Plot the histogram using hist() method.Increase "i" by 1.ExampleIn [1]: from matplotlib import pyplot as plt In [2]: myData = [[7, 8, 1], [2, 5, 2]] In [3]: i = 0 In [4]: for data in myData:    ...: fig, ax = plt.subplots()    ...: print("data number i =", ... Read More

How to remove relative shift in Matplotlib axis?

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:38:34

1K+ Views

To remove relative shift in matplotlib axis, we can take the following steps −Plot a line with two input lists.Using gca() method, get the current axis and then return the X-axis instance. Get the formatter of the major ticker. To remove the relative shift, use set_useOffset(False) method.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([10, 101, 1001], [1, 2, 3]) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) plt.show()Output

Define the size of a grid on a plot using Matplotlib

Rishikesh Kumar Rishi
Updated on 12-May-2021 11:36:34

5K+ Views

To define the size of a grid on a plot, we can take the following steps −Create a new figure or activate an existing figure using figure() method.Add an axes to the figure as a part of a subplot arrangement.Plot a curve with an input list.Make x and y margins 0.To set X-grids, we can pass input ticks points.To lay out the grid lines in current line style, use grid(True) method.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 fig = plt.figure() ax = fig.add_subplot(111) ax.plot([0, 2, 5, 8, 10, 1, 3, 14], ... Read More

Getting information for bins in Matplotlib histogram function

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:52:52

704 Views

To get information for bins in matplotlib histogram function, we can take the following steps −Create a list of numbers for data and bins.Compute the histogram of a set of data using histogram() method.Get the hist and edges from the histogram (step 2).Find the frequency in a histogram.Make a bar with bins (Step 1) and freq (step 4) data.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 a = [-0.125, .15, 8.75, 72.5, -44.245, 88.45] bins = np.arange(-180, 181, 20) hist, edges = np.histogram(a, bins) freq = hist/float(hist.sum()) plt.bar(bins[:-1], freq, width=20, ... Read More

How to adjust the space between legend markers and labels in Matplotlib?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:50:36

3K+ Views

To adjust the space between legend markers and labels, we can use labelspacing in legend method.StepsPlot lines with label1, label2 and label3.Initialize a space variable to increase or decrease the space between legend markers and label.Use legend method with labelspacing in the arguments.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([0, 1], [0, 1.0], label='Label 1') plt.plot([0, 1], [0, 1.1], label='Label 2') plt.plot([0, 1], [0, 1.2], label='Label 3') space = 2 plt.legend(labelspacing=space) plt.show()Output

How to redefine a color for a specific value in a Matplotlib colormap?

Rishikesh Kumar Rishi
Updated on 11-May-2021 13:48:14

2K+ Views

To redefine a color for a specific value in matplotlib colormap, we can take the following steps −Get a colormap instance, defaulting to rc values if *name* is None using get_cmap() method, with gray colormap.Set the color for low out-of-range values when "norm.clip = False" using set_under() method.Using imshow() method, display data an image, i.e., on a 2D regular raster.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, cm plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True cmap = cm.get_cmap('gray') cmap.set_under('red') plt.imshow(np.arange(25).reshape(5, 5),    interpolation='none',    cmap=cmap,    vmin=.001) plt.show()OutputRead More

Advertisements