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 save a plot in Seaborn with Python (Matplotlib)?
To save a plot in Seaborn, we can use the savefig() method from Matplotlib. Since Seaborn is built on top of Matplotlib, we can save any Seaborn plot using this approach. Basic Syntax import matplotlib.pyplot as plt # Create your Seaborn plot # Then save it plt.savefig('filename.png') Example: Saving a Pairplot Let's create a pairplot and save it to a file − import seaborn as sns import pandas as pd import numpy as np import matplotlib.pyplot as plt # Set figure parameters plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ...
Read MoreCreate a legend with Pandas and Matplotlib.pyplot
To create a legend with Pandas and matplotlib.pyplot, we can plot DataFrame data and enable the legend parameter. The legend helps identify different data series in the plot. Steps to Create a Legend Set the figure size and adjust the padding between and around the subplots. Create a DataFrame with multiple columns for plotting. Plot the DataFrame using plot() method with legend=True. Display the figure using show() method. Example Let's create a bar chart with a legend showing two data series ? import pandas as pd from matplotlib import pyplot as plt ...
Read MoreFrequency plot in Python/Pandas DataFrame using Matplotlib
A frequency plot visualizes how often each value appears in a dataset. In Python, you can create frequency plots from Pandas DataFrames using Matplotlib's plotting capabilities. Basic Frequency Plot Here's how to create a simple frequency plot using value_counts() and Matplotlib ? import pandas as pd import matplotlib.pyplot as plt # Configure plot settings plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create a DataFrame df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]}) # Create frequency plot fig, ax = plt.subplots() df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency') plt.show() ...
Read MoreHow do I print a Celsius symbol with Matplotlib?
To print a Celsius symbol (°C) with Matplotlib, you can use LaTeX mathematical notation within text labels. The degree symbol is rendered using ^\circ in math mode. Basic Example with Celsius Symbol Here's how to display the Celsius symbol in axis labels ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Generate sample temperature data N = 10 temperature = np.random.rand(N) * 30 # Temperature in Celsius pressure = np.random.rand(N) * 100 # Pressure values # Create ...
Read MoreAutomated legend creation in Matplotlib
Matplotlib can automatically create legends for scatter plots using the legend_elements() method. This is particularly useful when plotting data with multiple categories or varying sizes. Basic Automated Legend The legend_elements() method extracts legend information from scatter plots ? import matplotlib.pyplot as plt import numpy as np # Set figure size plt.figure(figsize=(8, 6)) # Generate sample data N = 30 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.randint(1, 4, size=N) # 3 categories # Create scatter plot scatter = plt.scatter(x, y, c=colors, s=100, cmap='viridis') # Automatically create legend for colors legend ...
Read MoreHow to plot a nested pie chart in Matplotlib?
A nested pie chart displays hierarchical data with an outer ring representing main categories and an inner ring showing subcategories. Matplotlib's pie() function can create concentric pie charts by adjusting the radius and width parameters. Creating a Basic Nested Pie Chart Here's how to create a nested pie chart with outer and inner rings ? 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 subplot fig, ax = plt.subplots() # Define data and styling size = 0.3 vals = ...
Read MorePlotting power spectral density in Matplotlib
A Power Spectral Density (PSD) plot shows how the power of a signal is distributed across different frequencies. Matplotlib provides the psd() method to create these plots, which is useful for analyzing signal frequency content and noise characteristics. Basic PSD Plot Let's create a signal with noise and plot its power spectral density ? import matplotlib.pyplot as plt import numpy as np # Set figure parameters plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create time series data dt = 0.01 t = np.arange(0, 10, dt) nse = np.random.randn(len(t)) r = np.exp(-t / 0.05) ...
Read MoreHow to plot single data with two Y-axes (two units) in Matplotlib?
To plot single data with two Y-axes (two units) in Matplotlib, you can use twinx() to create a secondary Y-axis that shares the same X-axis. This is useful when plotting two datasets with different units or scales. Basic Setup First, let's understand the key steps ? Create the primary plot with the first dataset Use twinx() to create a twin Y-axis Plot the second dataset on the twin axis Add appropriate labels and legends Example Here's a complete example plotting speed and acceleration with different Y-axis scales ? import matplotlib.pyplot as ...
Read MoreHow to reverse the colormap of an image to scalar values in Matplotib?
To reverse the colormap of an image in Matplotlib, you can use the .reversed() method on any colormap object. This creates a new colormap with inverted colors, which is useful for changing the visual emphasis or meeting specific design requirements. Steps to Reverse a Colormap Set the figure size and adjust the padding between and around the subplots Create data points using numpy arrays Get the desired colormap using plt.cm.get_cmap() method Create subplots to compare original and reversed colormaps Plot data points using scatter() method with original colormap Plot the same data with reversed colormap using .reversed() ...
Read MoreHow to embed an interactive Matplotlib plot on a webpage?
To create an interactive plot on a webpage, we can use Bokeh, which generates HTML files with JavaScript interactivity. This allows users to pan, zoom, and interact with plots directly in their browser. Installation and Setup First, install Bokeh if you haven't already ? pip install bokeh Basic Interactive Plot Example Here's how to create a simple interactive scatter plot ? from bokeh.plotting import figure, show, output_file import numpy as np # Configure output to HTML file output_file('interactive_plot.html') # Generate sample data x = np.random.random(100) * 100 y = ...
Read More