Found 784 Articles for Data Visualization

How to plot a rectangle on a datetime axis using Matplotlib?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:09:23

1K+ Views

To plot a recatangle on a datetime axis using 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 using add_subplot() method.To define a rectangle, find the anchor points using datetime and matplotlib's dates.Add a '~.Patch' to the axes' using add_patch() method.Set major axis locator and formatter.Limit x and y axes scale.To display the figure, use show() method.Examplefrom datetime import datetime, timedelta from matplotlib.patches import Rectangle import matplotlib.pyplot as plt ... Read More

How to remove scientific notation from a Matplotlib log-log plot?

Rishikesh Kumar Rishi
Updated on 03-Jun-2021 09:06:49

3K+ Views

To remove scientific notation from a matplotlib log-log plot, we can use ax.xaxis.set_minor_formatter(mticker.ScalarFormatter()) statement.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 scatter() method.Set x and y axes sacle using set_xscale() and set_yscale() methods.To remove scientific notation, use format tick values as a number.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt, ticker as mticker plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([1, 7, 6, 4, 0]) y = np.array([6, 2, 3, ... Read More

How to turn off the upper/right axis tick marks in Matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:50:54

1K+ Views

To turn off the upper or right axis ticks marks in matplotlib, we can make a custom dictionary visible_ticks and turn off the flag.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.Make a dictionary to turn off the axis ticks marks.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 x = np.linspace(-2, 2, 10) y = np.sin(x) plt.plot(x, y) visible_ticks = { "top": False, ... Read More

How to work with images in Bokeh (Python)?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:49:14

609 Views

To work with images in Bokeh, use image_url() method and pass a list of images.StepsConfigure the default output state to generate output saved to a file when :func:'show' is called.Create a new Figure for plotting.Render the images loaded from the given URLs.Immediately display a Bokeh object or application.Examplefrom bokeh.plotting import figure, show, output_file output_file('image.html') p = figure(x_range=(0, 1), y_range=(0, 1)) p.image_url(url=['bird.jpg'], x=0, y=1, w=0.8, h=0.6) show(p)Output

How to show legend elements horizontally in Matplotlib?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:47:03

5K+ Views

To show legend elements horizontally, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.Using plot() method, plot lines with the labels line1, line2 and line3.Place a legend on the figure using legend() method, with number of labels for ncol value in the argument.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 line1, = plt.plot([1, 2, 3], label="line1") line2, = plt.plot([3, 2, 1], label="line2") line3, = plt.plot([2, 3, 1], label="line3") plt.legend(ncol=3, loc="upper right") plt.show()OutputRead More

How to change the color of a single bar if a condition is true (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:45:08

5K+ Views

To change the color of a single bar if a condition is true, we can make a set of values and a list of colors with red until the value is 2; else add yellow color in the list.StepsSet the figure size and adjust the padding between and around the subplots.Initialize a variable width of a bar.Make two lists of values and colors.Use bar() method to plot bars.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 data = np.arange(5) width = 0.5 vals = [1, 2, 1, 5, ... Read More

How to annotate each cell of a heatmap in Seaborn?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:43:16

1K+ Views

To annotate each cell of a heatmap, we can make annot = True in heatmap() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with 5 columns.Use sns.heatmap() to plot a dataframe (Step 2) with annot=True flag in the argument.To display the figure, use show() method.Exampleimport seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"]) sns.heatmap(df, annot=True, annot_kws={"size": 7}) plt.show()OutputRead More

How to plot multiple Pandas columns on the Y-axis of a line graph (Matplotlib)?

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:41:47

1K+ Views

To plot multiple Pandas columns on the Y-axis of a line graph, we can set the index using set_index() method.StepsSet the figure size and adjust the padding between and around the subplots.Create a dataframe with Category 1, Category 2, and Category 3 columns.Use set_index() method to set the DataFrame index using existing columns.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 df = pd.DataFrame({'Category 1': [2, 4, 5, 1, 0, 3], 'Category 2': [6, 3, 1, 4, 5, 2], 'Category 3': [2, 4, 1, 3, 6, 0]}) df.set_index('Category 1').plot() plt.show()Output

How do you create line segments between two points in Matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Sep-2023 07:43:29

37K+ Views

To create line segments between two points in matplotlib, we can take the following stepsSet the figure size and adjust the padding between and around the subplots.To make two points, create two lists.Extract x and y values from point1 and point2.Plot x and y values using plot() method.Place text for both the points.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 point1 = [1, 2] point2 = [3, 4] x_values = [point1[0], point2[0]] y_values = [point1[1], point2[1]] plt.plot(x_values, y_values, 'bo', linestyle="--") plt.text(point1[0]-0.015, point1[1]+0.25, "Point1") plt.text(point2[0]-0.050, point2[1]-0.25, "Point2") plt.show()OutputRead More

Matplotlib Backend Differences between Agg and Cairo

Rishikesh Kumar Rishi
Updated on 02-Jun-2021 08:37:49

375 Views

RendererFile typesDescriptionAGGPngRaster graphics − high-quality images using the Anti-Grain Geometry engineCairopng, ps, pdf, svgRaster or vector graphics − using the Cairo libraryStepsSet the figure size and adjust the padding between and around the subplots.Set the backend name as "Agg".Create a 5☓5 matrix array using numpy.Use imshow() method to display data as an image, i.e., on a 2D regular raster.To save the figure, use savefig() method.Exampleimport matplotlib as mpl import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True mpl.use("Agg") data = np.random.rand(5, 5) plt.imshow(data, interpolation='nearest', cmap="copper") plt.savefig('agg.png')OutputRead More

Advertisements