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 plot thousands of circles quickly in Matplotlib?
When plotting thousands of circles in Matplotlib, using individual Circle patches becomes very slow. The efficient approach is to use CircleCollection from matplotlib.collections, which renders all circles in a single operation. Why Use CircleCollection? CircleCollection is optimized for rendering many similar shapes at once. Instead of creating thousands of individual patches, it handles all circles as a single collection, dramatically improving performance. Basic Implementation import numpy as np import matplotlib.pyplot as plt import matplotlib.collections as mc # Generate random data for 1000 circles num_circles = 1000 sizes = 50 * np.random.random(num_circles) positions = 10 ...
Read MoreHow to plot a time series graph using Seaborn or Plotly?
Time series graphs help visualize data changes over time. Seaborn and Plotly are powerful Python libraries that make creating time series plots straightforward and visually appealing. Using Seaborn for Time Series Plot Seaborn's lineplot() function creates clean time series visualizations with minimal code ? import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [10, 6] plt.rcParams["figure.autolayout"] = True # Create sample time series data df = pd.DataFrame({ 'time': pd.date_range("2021-01-01 12:00:00", periods=10, freq="30min"), 'speed': ...
Read MoreHow to make a rug plot in Matplotlib?
A rug plot is a one-dimensional visualization that displays data points as marks along an axis, making it easy to see the distribution and density of values. In Matplotlib, rug plots are often combined with density curves to provide a comprehensive view of data distribution. Basic Rug Plot Let's start with a simple rug plot using sample data points ? import numpy as np import matplotlib.pyplot as plt # Sample data points data = np.array([-6, -4, 2, 1, 4], dtype=np.float) # Create figure and axis fig, ax = plt.subplots(figsize=(8, 4)) # Create rug ...
Read MoreHow to fill rainbow color under a curve in Python Matplotlib?
Creating a rainbow gradient effect under a curve in Matplotlib involves using the fill_between() function with multiple color layers. This technique creates visually appealing plots by stacking colored regions with slightly different y-offsets. Basic Rainbow Fill Implementation The key is to plot multiple fill_between() layers, each with a different color and y-offset ? import numpy as np import matplotlib.pyplot as plt def plot_rainbow_under_curve(): rainbow_colors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red'] x = np.linspace(-5, 5, 100) y = x ** 2 ...
Read MoreHow to draw axis lines inside a plot in Matplotlib?
In Matplotlib, you can draw axis lines inside a plot by positioning the spines at zero and hiding unnecessary borders. This creates a coordinate system where the x and y axes pass through the origin. Understanding Spines Spines are the lines connecting the axis tick marks that form the boundaries of the data area. By default, Matplotlib shows all four spines (top, bottom, left, right) around the plot area. Drawing Axis Lines Inside the Plot Here's how to position the axis lines at the center of the plot ? import numpy as np import ...
Read MoreHow to set same scale for subplots in Python using Matplotlib?
When creating subplots in Matplotlib, you often want them to share the same scale for better comparison. This is achieved using the sharex and sharey parameters when creating subplot arrangements. Using sharex Parameter The sharex parameter links the x-axis scale across subplots. When you zoom or pan one subplot, all linked subplots will follow ? import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create figure fig = plt.figure() # Add subplots with shared x-axis ax1 = fig.add_subplot(2, 1, 1) ax2 ...
Read MoreConditional removal of labels in Matplotlib pie chart
To remove labels from a Matplotlib pie chart based on a condition, you can use a lambda function in the autopct parameter. This technique is useful when you want to display percentage labels only for slices that meet certain criteria, such as being above a specific threshold. Steps to Conditionally Remove Labels Set the figure size and adjust the padding between and around the subplots Create a Pandas DataFrame with your data Plot a pie chart using pie() method with conditional removal of labels Use a lambda function in autopct to show labels only when conditions are ...
Read MoreMatplotlib – Make a Frequency histogram from a list with tuple elements in Python
To make a frequency histogram from a list with tuple elements in Python, we can extract the categories and frequencies from tuples and create a bar chart using Matplotlib. Steps to Create a Frequency Histogram Set the figure size and adjust the padding between and around the subplots Create a list of tuples containing category-frequency pairs Extract categories and frequencies by iterating through the data Create a bar plot using bar() method Display the figure using show() method Example Here's how to create a frequency histogram from tuple data ? import matplotlib.pyplot ...
Read MoreHow to rotate a simple matplotlib Axes?
To rotate a simple matplotlib axes, we can use the Affine2D transformation along with floating_axes. This technique creates a rotated coordinate system for plotting data at different angles. Required Imports First, import the necessary packages for creating rotated axes ? import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D import mpl_toolkits.axisartist.floating_axes as floating_axes Steps to Rotate Axes The rotation process involves these key steps: Create an affine transformation − Define the rotation angle using Affine2D().rotate_deg() Set axis limits − Define the coordinate range for both x and y axes Create grid helper ...
Read MoreHow to add a 3d subplot to a matplotlib figure?
To add a 3D subplot to a matplotlib figure, you need to specify projection='3d' when creating the subplot. This enables three-dimensional plotting capabilities for visualizing data in 3D space. Basic Steps Follow these steps to create a 3D subplot ? Import matplotlib and numpy libraries Create x, y and z data points Create a figure using plt.figure() Add a subplot with projection='3d' parameter Plot the 3D data using appropriate plotting methods Display the figure with plt.show() Example: Creating a 3D Line Plot Here's how to create a basic 3D line plot ? ...
Read More