Matplotlib Articles

Page 67 of 91

Generating a movie from Python without saving individual frames to files

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 454 Views

Creating animated movies in Python using matplotlib's FuncAnimation allows you to generate smooth animations without saving individual frames to disk. This approach is memory-efficient and perfect for real-time particle simulations. Key Concepts The animation works by repeatedly calling an update function that modifies particle positions and returns updated plot elements. FuncAnimation handles the timing and display automatically. Steps to Create the Animation Initialize particles with position, velocity, force, and size properties Create a matplotlib figure with specified dimensions Add axes with appropriate x and y limits Create initial scatter plot for particle positions Define an ...

Read More

How to prevent numbers being changed to exponential form in Python Matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 1K+ Views

When plotting large numbers in Matplotlib, the axis labels often switch to scientific notation (exponential form) automatically. You can prevent this by using the ticklabel_format() with style='plain' parameter. Syntax plt.ticklabel_format(style='plain') The style='plain' parameter turns off scientific notation and displays numbers in their regular decimal format. Example Here's how to prevent exponential notation when plotting data ? import matplotlib.pyplot as plt # Plot data that would normally trigger scientific notation plt.plot([1, 2, 3, 4, 5], [11000, 12000, 13000, 14000, 15000]) # Prevent scientific notation on y-axis plt.ticklabel_format(style='plain') plt.title('Numbers in ...

Read More

Plotting dates on the X-axis with Python\'s Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 34K+ Views

Using Pandas, we can create a dataframe and set datetime values as the index. Matplotlib's gcf().autofmt_xdate() automatically formats date labels on the X-axis for better readability. Steps to Plot Dates on X-axis Create a list of date strings and convert them to datetime using pd.to_datetime() Prepare your data values (e.g., [1, 2, 3]) Create a DataFrame and assign the data to a column Set the DataFrame index using the datetime values Plot the DataFrame using plt.plot() Format the X-axis dates using plt.gcf().autofmt_xdate() Display the plot with plt.show() Example import pandas as pd import ...

Read More

Show only certain items in legend Python Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 2K+ Views

In Python Matplotlib, you can control which items appear in the legend by using the plt.legend() method with a list of labels. This allows you to show only specific plot elements in the legend rather than all plotted data. Basic Syntax The plt.legend() method accepts a list of labels to display ? plt.legend(["label1", "label2"], loc=location, frameon=True/False) Parameters labels − List of strings to show in the legend loc − Location of the legend (0 for best location) frameon − Boolean flag to show/hide legend border Example Here's how to ...

Read More

Manually add legend Items Python Matplotlib

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 23K+ Views

In Matplotlib, you can manually add legend items using the plt.legend() method. This allows you to create custom legends with specific labels, positions, and styling options like borders. Basic Syntax plt.legend(labels, loc=location, frameon=True/False) Parameters labels − List of strings for legend labels loc − Location of the legend (0 for best location) frameon − Boolean to show/hide the legend border Example Here's how to create a plot with manually added legend items ? import matplotlib.pyplot as plt # Set axis labels plt.xlabel("X-axis") plt.ylabel("Y-axis") # Plot two ...

Read More

Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 4K+ Views

Interactive 3D plots in Jupyter Notebook allow you to rotate, zoom, and pan your visualizations. Matplotlib provides built-in interactivity when using the %matplotlib notebook or %matplotlib widget magic commands. Setting Up Interactive Mode To enable interactivity, use the appropriate magic command at the beginning of your notebook ? %matplotlib notebook # or use %matplotlib widget for newer versions import matplotlib.pyplot as plt import numpy as np Creating an Interactive 3D Sphere Here's how to create an interactive 3D wireframe sphere ? import matplotlib.pyplot as plt import numpy as np # ...

Read More

How to have logarithmic bins in a Python histogram?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 25-Mar-2026 6K+ Views

In Python, creating a logarithmic histogram involves using logarithmically spaced bins instead of linear ones. This is particularly useful when your data spans several orders of magnitude. We can achieve this using NumPy for generating logarithmic bins and matplotlib for plotting. Logarithmic bins are spaced exponentially rather than linearly, making them ideal for data that follows power-law distributions or spans wide ranges. Basic Example with Logarithmic Bins Let's create a simple histogram with logarithmic bins ? import matplotlib.pyplot as plt import numpy as np # Create sample data data = np.random.exponential(scale=2, size=1000) # ...

Read More

Python xticks in subplots

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

Subplots allow you to display multiple plots in a single figure by dividing it into a grid. When working with subplots, you can customize the x-axis ticks for each subplot independently using plt.xticks(). Understanding Subplot Layout The plt.subplot() function creates subplots using three parameters: nrows, ncols, and index. For example, plt.subplot(121) creates a 1×2 grid and selects the first subplot. Basic Subplot with Different X-ticks Here's how to create two subplots with custom x-tick positions − import matplotlib.pyplot as plt line1 = [21, 14, 81] line2 = [31, 6, 12] # First ...

Read More

What's the fastest way of checking if a point is inside a polygon in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

Checking if a point is inside a polygon is a common computational geometry problem. Python offers several approaches, with matplotlib's Path class being one of the fastest and most reliable methods for this task. Using matplotlib.path for Point-in-Polygon Testing The matplotlib library provides an efficient implementation through the mplPath.Path class, which uses optimized algorithms for point-in-polygon testing. Steps Create a list of points to define the polygon vertices. Create a path object using mplPath.Path() with the polygon coordinates. Use the contains_point() method to check if a point lies inside the polygon. Example ...

Read More

How to plot ROC curve in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 7K+ Views

The ROC (Receiver Operating Characteristic) curve is a graphical plot used to evaluate binary classification models. It shows the trade-off between true positive rate (sensitivity) and false positive rate (1-specificity) at various threshold settings. Python's sklearn.metrics module provides the plot_roc_curve() method to easily visualize ROC curves for classification models. Steps to Plot ROC Curve Generate a random binary classification dataset using make_classification() method Split the data into training and testing sets using train_test_split() method Train a classifier (like SVM) on the training data using fit() method Plot the ROC curve using plot_roc_curve() method Display the plot ...

Read More
Showing 661–670 of 902 articles
« Prev 1 65 66 67 68 69 91 Next »
Advertisements