Data Visualization Articles

Page 36 of 68

How to embed fonts in PDFs produced by Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 16-Jun-2021 2K+ Views

To embed fonts in PDFs produced by Matplotlib, we can use rc.Params['pdf.fonttype']=42.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.Create x and y data points using numpy.Plot x and y data points using scatter() method.Set the title of the plot.Save the figure in pdf format.Exampleimport numpy as np from matplotlib import pyplot as plt, font_manager as fm plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams['pdf.fonttype'] = 42 fig, ax = plt.subplots() x = np.random.rand(100) y = np.random.rand(100) ax.scatter(x, y, c=y, marker="v") ...

Read More

How to animate the colorbar in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 16-Jun-2021 2K+ Views

To animate the colorbar in 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 '~.axes.Axes' to the figure as part of a subplot arrangement.Instantiate Divider based on the pre-existing axes, i.e., ax object and return a new axis locator for the specified cell.Create an axes at the given *position* with the same height (or width) of the main axes.Create random data using numpy.Use imshow() method to plot random data.Set the title of the plot.Instantiate the list of colormaps.To animate the ...

Read More

Matplotlib animation not working in IPython Notebook?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 16-Jun-2021 3K+ Views

To animate a plot in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a random data of shape 10X10 dimension.Create a figure and a set of subplots, using subplots() method.Makes an animation by repeatedly calling a function *func*, using FuncAnimation() class.To update the contour value in a function, we can define a method animate that can be used in FuncAnimation() class.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = ...

Read More

How to display the count over the bar in Matplotlib histogram?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 4K+ Views

To display the count over the bar in matplotlib histogram, we can iterate each patch and use text() method to place the values over the patches.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of numbers to make a histogram plot.Use hist() method to make histograms.Iterate the patches and calculate the mid-values of each patch and height of the patch to place a text.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True data = [3, 5, 1, 7, 9, 5, 3, 7, 5] _, ...

Read More

How to replace auto-labelled relative values by absolute values in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 799 Views

To replace auto-labelled relayive values by absolute values in matplotlib, we can use autopct=lambda p: .StepsSet the figure size and adjust the padding between and around the subplots.Make lists of labels, fractions, explode position and get the sum of fractions to calculate the percentage.Make a pie chart using labels, fracs and explode with autopct=lambda p: .To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True labels = ('Read', 'Eat', 'Sleep', 'Repeat') fracs = [5, 3, 4, 1] total = sum(fracs) explode = (0, 0.05, 0, 0) plt.pie(fracs, explode=explode, labels=labels,   ...

Read More

How to make simple double head arrows on the axes in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 4K+ Views

To make simple double head arrows on the axes in Matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Use annotate() method to annotate the point xy with text='Arrows'. Start the tuple and end it for positions. In arrowprops dictionary, use arrowstyle "" and color='red'.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.annotate('Arrows', xy=(0.1, .1), xytext=(0.5, 0.5),             arrowprops=dict(arrowstyle='', color='red')) plt.show()Output

Read More

How to add legends and title to grouped histograms generated by Pandas? (Matplotlib)

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 4K+ Views

To add legends and title to grouped histograms generated by Pandas, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with "a", "b", "c" and "d" keys.Plot data frame with kind="hist"Set a title for the axes.To display the figure, use show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'a': [1, 1, 1, 1, 3],                   'b': [1, 1, 2, 1, 3],                   'c': [2, 2, 2, 1, 3],                   'd': [2, 1, 2, 1, 3], }) df.plot(kind='hist') plt.title("Grouped Histograms") plt.show()Output

Read More

Plot scatter points on polar axis in Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 1K+ Views

To plot scatter points on polar axis 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, for number of sample data.Get r, theta, area and color data using numpyCreate a new figure or activate an existing figure.Plot theta, r, colors and area, using scatter() method.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 N = 150 r = 2 * np.random.rand(N) theta = 2 * np.pi * np.random.rand(N) area = 200 ...

Read More

How do I omit Matplotlib printed output in Python / Jupyter notebook?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 3K+ Views

To omit matplotlib printed output in Python/Jupeter notebook, we can take the following steps −import numpy as np.from matplotlib import pyplot as pltCreate points for x, i.e., np.linspace(1, 10, 1000)Now, plot the line using plot() method.To hide the instance, use plt.plot(x); (with semi-colon)Or, use _ = plt.plot(x).ExampleIn [1]: import numpy as np In [2]: from matplotlib import pyplot as plt In [3]: x = np.linspace(1, 10, 1000) In [4]: plt.plot(x) Out[4]: [] In [5]: plt.plot(x); In [6]: _ = plt.plot(x) In [7]:OutputOut[4]: []

Read More

How to save figures to pdf as raster images in Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 15-Jun-2021 1K+ Views

To save figures to pdf as raster images in 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 axes to the figure as part of a subplot arrangement.Create random data using numpy.Display the data as an image, i.e., on a 2D regular raster.Save the plot as pdf format.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 = fig.add_subplot(111, rasterized=True) data = np.random.rand(5, 5) ax.imshow(data, cmap="copper", aspect=True, interpolation="nearest") ...

Read More
Showing 351–360 of 680 articles
« Prev 1 34 35 36 37 38 68 Next »
Advertisements