Found 784 Articles for Data Visualization

Setting the same axis limits for all subplots in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:45:40

13K+ Views

To set the same axis limits for all subplots in matplotlib we can use subplot() method to create 4 subplots where nrows=2, ncols=2 having share of x and y axes.StepsSet the figure size and adjust the padding between and around the subplots.Add a subplot to the current figure at index 1.Set the x and y axes view limit using set_xlim() and set_ylim() methods.Plot a line on axis 1 (step 2).Add a subplot to the current figure at index 2 with the same limit (step 3).Plot a line on axis 2.Add a subplot to the current figure at index 3 with ... Read More

Logscale plots with zero values in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:45:00

1K+ Views

To logscale plots with zero values in matplotlib, we can use xscale() and yscale() methods with "symlog" class by name.StepsSet the figure size and adjust the padding between and around the subplots.Plot two lists containing zero values using plot() method.Use yscale() method with "symlog" class by name.Use xscale() method with "symlog" class by name.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 plt.plot([0, 1, 2, 0, 3], [1, 0, 2, 3, 5], marker='o', linestyle='-') plt.yscale('symlog') plt.xscale('symlog') plt.show()Output

Adjusting Text background transparency in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:40:27

2K+ Views

To adjust text background transparency in matplotlib, we can change the alpha value in the dictionary of bbox with facecolor='red' and alpha='0.4'.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Plot x and y data points using plot() method.Now use text() method to adjust the text background with fontdict and bbox dictionaries at x=-1.0 and y=4.0.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 x = np.linspace(-2, 2, 10) y = np.exp(x) plt.plot(x, y) plt.text(-1.0, 4.0, ... Read More

How to plot a dashed line on a Seaborn lineplot in Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:44:18

5K+ Views

To plot a dashed line on a Seaborn lineplot, we can use linestyle="dashed" in the argument of lineplot().StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Use lineplot() method with x and y data points in the argument and linestyle="dashed".To display the figure, use show() method.Exampleimport seaborn as sns import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(10) y = np.random.rand(10) ax = sns.lineplot(x=x, y=y, linestyle="dashed") plt.show()OutputRead More

How to skip empty dates (weekends) in a financial Matplotlib Python graph?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:44:01

926 Views

To skip weekends in a financial graph in matplotlib, we can iterate the time in dataframe and skip the plot if weekday is 5 or 6.StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with keys time.Iterate zipped index and time of a date frame.If iterated timestamp is having weekday 5 or 6, don't plot them.Other than 5 or 6 weekday, plot the points.Set the current tick locations of Y-axis.Lay out a plot with grid lines.To display the figure, use show() method.Exampleimport pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, ... Read More

How to write annotation outside the drawing in data coords in Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:44

1K+ Views

We can use annotate() method to place annotation outside the drawing.StepsSet 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 using subplots() method.Use scatter() method to plot x and y data points using star marker and copper color map.To place annotation outside the drawing, use xy coordinates tuple accordingly.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 x = np.random.rand(100) y = np.random.rand(100) fig, ax = plt.subplots() ax.scatter(x, y, ... Read More

Histogram for discrete values with Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:20

2K+ Views

To plot a histogram for discrete values with matplotlib, we can use hist() method.StepsSet the figure size and adjust the padding between and around the subplots.Make a list of discrete values.Use hist() method to plot data with bins=length of data and edgecolor=black.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 data = [1, 4, 2, 3, 5, 9, 6, 7] plt.hist(data, bins=len(data), edgecolor='black') plt.show()Output

Plotting only the upper/lower triangle of a heatmap in Matplotlib

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:43:04

998 Views

To plot only the upper/lower triangle of a heatmap in matplotlib, we can use numpy to get the masked 2D array and convert them into an image to produce a heatmap.StepsSet the figure size and adjust the padding between and around the subplots.Create a random data of 5×5 dimension.Use numpy.tri() method to create an array with 1's at and below the given diagonal and 0's elsewhere.Get the masked 2D array data with masked array (Using step 3).Use imshow() method to display the data as an image, i.e., on a 2D regular raster.To display the figure, use show() method.Exampleimport numpy as ... Read More

Is it possible to control Matplotlib marker orientation?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:42:45

381 Views

To control matplotlib marker orientation, we can use marker tuple that contains a number of sides, style and rotation or orientation of the marker.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points using numpy.Make an array of 10 different rotations.Zip x, y and i. Iterate them and plot the points using plot() method with marker tuple.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 x = np.random.rand(10) y = np.random.rand(10) i = np.linspace(0, 10, 10) for x, ... Read More

How to change the linewidth of a hatch in Matplotlib?

Rishikesh Kumar Rishi
Updated on 01-Jun-2021 11:40:06

2K+ Views

To change the linewidth of a hatch in matplotlib, we can set the linewidth of the hatch in the params.StepsSet the figure size and adjust the padding between and around the subplots.Create x and y=sin(x) data points using numpy.Set the linewidth of the hatch in the plot.Plot x and y data points using scatter() method with a square marker having "/" hatches with set linewidth.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 x = np.linspace(-5, 5, 25) y = np.sin(x) plt.rcParams['hatch.linewidth'] = 1 plt.scatter(x, y, ... Read More

Advertisements