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 by Rishikesh Kumar Rishi
Page 57 of 102
Setting the same axis limits for all subplots in Matplotlib
Setting the same axis limits for all subplots in Matplotlib ensures consistent scaling across multiple plots. You can achieve this using sharex and sharey parameters or by explicitly setting limits on each subplot. Method 1: Using sharex and sharey Parameters The most efficient approach is to share axes between subplots ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] = True # Create first subplot and set limits ax1 = plt.subplot(2, 2, 1) ax1.set_xlim(0, 5) ax1.set_ylim(0, 5) ax1.plot([1, 4, 3], 'b-o', label='Plot 1') ax1.set_title('Subplot 1') ...
Read MoreLogscale plots with zero values in Matplotlib
Matplotlib's logarithmic scaling fails when data contains zero values because log(0) is undefined. The symlog (symmetric logarithm) scale solves this by using linear scaling near zero and logarithmic scaling for larger values. Understanding Symlog Scale The symlog scale combines linear and logarithmic scaling. It uses linear scaling within a threshold around zero and logarithmic scaling beyond that threshold, making it perfect for data containing zero values. Basic Example with Zero Values Here's how to create a symlog plot with data containing zeros − import matplotlib.pyplot as plt # Data with zero values x ...
Read MoreHow to plot a dashed line on a Seaborn lineplot in Matplotlib?
To plot a dashed line on a Seaborn lineplot, we can use linestyle="dashed" in the argument of lineplot(). This creates visually distinct line patterns that are useful for differentiating multiple data series. Steps Set 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. Example import seaborn as sns import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ...
Read MoreHow to skip empty dates (weekends) in a financial Matplotlib Python graph?
When creating financial charts in Matplotlib, you often need to skip weekends (Saturday and Sunday) to display only business days. This prevents gaps in your time series visualization and provides a cleaner representation of trading data. Understanding Weekdays in Python Python's weekday() method returns integers where Monday=0, Tuesday=1, ..., Saturday=5, Sunday=6. To skip weekends, we check if weekday() returns 5 (Saturday) or 6 (Sunday). Basic Example Here's how to plot only weekdays while skipping weekends ? import pandas as pd import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] ...
Read MoreHow to write annotation outside the drawing in data coords in Matplotlib?
In Matplotlib, you can place annotations outside the plotting area using the annotate() method with specific coordinate systems. This is useful for adding titles, labels, or explanatory text that shouldn't interfere with your data visualization. Understanding Coordinate Systems Matplotlib offers different coordinate systems for positioning annotations ? Data coordinates: Based on your actual data values Axes coordinates: Relative to the axes (0-1 range) Transform coordinates: Using axis transforms for positioning outside plot area Basic Example with Transform Coordinates Here's how to place an annotation ...
Read MoreHistogram for discrete values with Matplotlib
To plot a histogram for discrete values with Matplotlib, we can use the hist() method. Discrete histograms are useful for visualizing the frequency distribution of categorical or integer data points. Steps Set the figure size and adjust the padding between and around the subplots. Create 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. Example Let's create a histogram for discrete values with proper bin configuration ? ...
Read MorePlotting only the upper/lower triangle of a heatmap in Matplotlib
To plot only the upper or lower triangle of a heatmap in Matplotlib, we can use NumPy to create a mask that hides specific parts of the data. This technique is useful for correlation matrices where the upper and lower triangles contain identical information. Plotting Lower Triangle Heatmap We use numpy.tri() to create a triangular mask and apply it to our data − 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 random 5x5 data data = np.random.rand(5, 5) # Create ...
Read MoreIs it possible to control Matplotlib marker orientation?
To control matplotlib marker orientation, we can use marker tuples that contain the number of sides, style, and rotation angle of the marker. This technique is particularly useful for creating custom arrow-like markers or rotating polygonal shapes. Understanding Marker Tuples A marker tuple follows the format (numsides, style, angle) where − numsides − Number of sides for the polygon marker style − Style of the marker (0 for regular polygon) angle − Rotation angle in degrees Basic Example with Fixed Rotation Let's start with a simple example showing triangular markers with different orientations ...
Read MoreHow to change the linewidth of a hatch in Matplotlib?
To change the linewidth of a hatch in matplotlib, we can set the linewidth of the hatch using plt.rcParams['hatch.linewidth']. This parameter controls how thick or thin the hatch lines appear in your plots. Steps Set 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 using plt.rcParams['hatch.linewidth'] Plot x and y data points using scatter() method with a square marker having "/" hatches with set linewidth Display the figure using show() method Example Here's how to create a ...
Read MoreAdd a custom border to certain cells in a Matplotlib / Seaborn plot
Adding custom borders to specific cells in Matplotlib/Seaborn plots helps highlight important data points. You can use Rectangle patches to create colored borders around target cells in heatmaps. Basic Approach The process involves creating a heatmap, accessing its axes, and adding rectangle patches with custom colors and line widths ? import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # Create sample data data = pd.DataFrame({ "col1": [1, 4, 2, 3, 5], "col2": [3, 4, 1, 5, 2] }) # Create clustered ...
Read More