Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to make axes transparent in Matplotlib?
To make axes transparent in Matplotlib, you can use the set_alpha() method or patch.set_alpha() to control the transparency level. A lower alpha value creates more transparency, while higher values make the axes more opaque. Basic Axes Transparency Here's a simple example showing how to create transparent axes ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [8, 6] plt.rcParams["figure.autolayout"] = True # Create figure and axes fig, ax = plt.subplots() # Create sample data x = np.linspace(0, 10, 100) y = np.sin(x) # Plot the data ...
Read MoreHow to name different lines in the same plot of Matplotlib?
To name different lines in the same plot of matplotlib, we can use the label parameter in the plot() method and display them with legend(). This helps distinguish between multiple data series on the same plot. Steps to Name Different Lines Set the figure size and adjust the padding between and around the subplots. Create data points for different lines. Plot each line using plot() method with unique label parameter. Add a legend to display the line names using legend(). Display the figure using show() method. Example Here's how to plot multiple lines with ...
Read MoreDrawing circles on an image with Matplotlib and NumPy
Drawing circles on an image combines image processing with matplotlib's patch functionality. This technique is useful for highlighting regions of interest, marking detected objects, or creating visual annotations. Basic Circle Drawing Here's how to draw circles on a sample image using matplotlib patches ? import matplotlib.pyplot as plt import numpy as np from matplotlib.patches import Circle # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Read an image from file img = plt.imread('bird.jpg') # Generate random positions for circles x = np.random.rand(5) * img.shape[1] y = np.random.rand(5) * ...
Read MoreHow to change the legend fontname in Matplotlib?
To change the legend fontname in Matplotlib, you can use several approaches. The most common methods are using the fontname parameter in legend() or setting font properties for individual legend text elements. Method 1: Using fontname Parameter The simplest way is to specify the fontname parameter directly in the legend() method ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(-5, 5, 100) # Plot functions plt.plot(x, np.sin(x), label="y=sin(x)") plt.plot(x, np.cos(x), label="y=cos(x)") # Add legend ...
Read MoreAdding units to heatmap annotation in Seaborn
To add units to a heatmap annotation in Seaborn, we can customize the text annotations after creating the heatmap. This is useful for displaying data with specific units like percentages, currency, or measurements. Steps to Add Units Set the figure size and adjust the padding between and around the subplots. Create a 5×5 dimension matrix using NumPy. Plot rectangular data as a color-encoded matrix. Annotate heatmap values with %age unit. To display the figure, use show() method. Example Here's how to create a heatmap and add percentage units to the annotations − ...
Read MoreHow to color a Matplotlib scatterplot using a continuous value?
To color a matplotlib scatterplot using continuous values, we can map a third variable to the color of each point. This creates a visual representation where color intensity or hue represents the magnitude of the continuous variable. Basic Scatterplot with Continuous Coloring Here's how to create a scatter plot where colors represent continuous values ? import numpy as np import matplotlib.pyplot as plt # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Generate random data points x, y, z = np.random.rand(3, 50) # Create figure and subplots f, ...
Read MoreText alignment in a Matplotlib legend
Text alignment in a Matplotlib legend allows you to control how the legend text is positioned. You can set horizontal alignment using the set_ha() method on legend text objects. Basic Legend Text Alignment Here's how to align legend text to the left ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-5, 5, 100) plt.plot(x, np.sin(x), label="$y=sin(x)$") plt.plot(x, np.cos(x), label="$y=cos(x)$") legend = plt.legend(loc='upper right') for t in legend.get_texts(): t.set_ha('left') plt.show() Different Alignment Options You ...
Read MorePlot Matplotlib 3D plot_surface with contour plot projection
To create a 3D surface plot with contour projections in Matplotlib, we combine plot_surface() for the main surface and contourf() for projecting contours onto the coordinate planes. Understanding the Components A surface plot with contour projections consists of: A 3D surface using plot_surface() Contour projections on the XY, XZ, and YZ planes using contourf() The zdir parameter controls which plane the contour is projected onto Basic Example Here's how to create a surface plot with contour projections ? import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import ...
Read MoreHow to add Matplotlib Colorbar Ticks?
A colorbar in Matplotlib displays the color scale used in a plot. By default, Matplotlib automatically places ticks on the colorbar, but you can customize these ticks to show specific values or improve readability. Basic Colorbar with Custom Ticks Here's how to add custom ticks to a colorbar using np.linspace() ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data x, y = np.mgrid[-1:1:100j, -1:1:100j] z = (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2)) ...
Read MoreHow to change the font properties of a Matplotlib colorbar label?
To change the font properties of a Matplotlib colorbar label, you can customize the label using various font parameters like weight, size, and family. Here's how to modify colorbar label properties effectively. Steps to Change Colorbar Label Font Properties Set the figure size and adjust the padding between and around the subplots. Create x, y and z data points using numpy. Use imshow() method to display the data as an image, i.e., on a 2D regular raster. Create a colorbar for a ScalarMappable instance, mappable. Using colorbar axes, set the font properties such that the label is ...
Read More