Found 784 Articles for Data Visualization

How to set the margins of a Matplotlib figure?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:06:23

5K+ Views

To set the margins of a matplotlib figure, we can use margins() method.StepsSet the figure size and adjust the padding between and around the subplots.Create t and y data points using numpy.Add a subplot to the current figure at index 1.Plot t and y data points using plot() method.Set the title of the plot.Add a subplot to the current figure at index 2.Plot t and y data points using plot() method.Set the title of the plot.Set margins of the plot using margins(x=0, y=0).To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] ... Read More

Automatically setting Y-axis limits for a bar graph using Matplotlib

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:00:21

8K+ Views

To set Y-axis limit, we can use ylim() method and put maximum and minimum limit values.StepsSet the figure size and adjust the padding between and around the subplots.Create two lists for data points.Make two variables for max and min values for Y-axis.Use ylim() method to limit the Y-axis range.Use bar() method to plot the bars.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = [1, 2, 3, 4, 5] y = [8, 4, 6, 1, 3] max_y_lim = max(y) + .5 min_y_lim = min(y) plt.ylim(min_y_lim, max_y_lim) plt.bar(x, y) ... Read More

How do you improve Matplotlib image quality?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:16:42

6K+ Views

To improve matplotlib image quality we can use greater dot per inch i.e dpi value (greater than 600) and pdf or .eps format can be recommended.StepsSet the figure size and adjust the padding between and around the subplots.Make a 2D data raster using a np.array.Display data as an image, i.e., on a 2D regular raster.Save the current image using savefig() with dpi=1200 and .eps format, 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 data = np.array( [[0.1, 0.7, 0.6, 0.3], ... Read More

How to close a Python figure by keyboard input using Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:14:27

1K+ Views

To close a Python figure by a keyboard input, we can use plt.pause() method, an input, and close() method.StepsSet the figure size and adjust the padding between and around the subplots.Create random t and y data points using numpy.Create a new figure or activate an existing figure using figure() method.Plot t and y data points using plot() method.Set the title of the plot.Redraw the current figure using draw() method.Run a true loop to pause the current figure.Take input from the user to go to the next statement.Use close() method to close the figure.Exampleimport numpy as np from matplotlib import pyplot ... Read More

How can I display an np.array with pylab.imshow() using Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:14:10

916 Views

To display an np.array with imshow(), we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Make a 2D data raster using an np.array.Display the data as 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 plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = np.array( [[0.1, 0.7, 0.6, 0.3], [0.2, 0.6, 0.5, 0.2], [0.8, 0.3, 0.80, 0.01], [0.3, 0.4, 0.2, 0.1]] ) plt.imshow(data, interpolation="nearest", cmap="RdYlGn_r") plt.show()Output

How to plot a stacked event duration using Python Pandas?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:15:04

374 Views

To plot a stacked event duration using Python Pandas, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with lists of xmin and its corresponding xmax.Use hlines() method to plot a stacked event duration.To display the figure, use show() method.Exampleimport pandas as pd from datetime import datetime as dt from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(xmin=[dt.strptime('1994-07-19', '%Y-%m-%d'), dt.strptime('2006-03-16', '%Y-%m-%d'), dt.strptime('1980-10-31', '%Y-%m-%d'), dt.strptime('1981-06-11', '%Y-%m-%d'), dt.strptime('2006-06-28', '%Y-%m-%d')], ... Read More

What is the simplest way to make Matplotlib in OSX work in a virtual environment?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:15:36

73 Views

To make matplotlib in OSX work in a virtual environment, we can first create a virtual environment and then activate that created environment. Thereafter, install all the dependencies in that virtual environment.StepsOpen ubuntu terminal.apt-get install python-venvpython -m venv source /bin/activate

Annotate Subplots in a Figure with A, B, C using Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:16:14

1K+ Views

To annotate subplots in a figure with A, B and C using matplotlib, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots, with nrows=1 and ncols=3.Make a 1D iterator over an array.Iterate each axes and display data as an image.In the loop itself, place text A, B and C.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt import string plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, axs = plt.subplots(1, 3) axs = axs.flat for index, ax ... Read More

Connecting two points on a 3D scatter plot in Python and Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:12:56

6K+ Views

To connect two points on a 3D scatter plot, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure using figure() method.Add an axes to the current figure as a subplot arrangement.Create lists for x, y and z.Plot x, y and z data points using scatter() methodTo connect the points, use plot() method with x, y and z data points with black color line.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = ... Read More

Controlling the alpha value on a 3D scatter plot using Python and Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 12:12:08

543 Views

To control the alpha value on a 3D scatter plot using Python and Matplotlib, we can set the facecolor and edgecolors value.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.Add an '~.axes.Axes' to the figure as part of a subplot arrangement.Create x, y and z data points using numpy.Plot x, y and z points using scatter() method.Set the facecolors and edgecolors.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ... Read More

Advertisements