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
Server Side Programming Articles
Page 378 of 2109
Save figure as file from iPython notebook using Matplotlib
To save a figure as a file from iPython notebook using Matplotlib, we can use the savefig() method. This method allows you to export plots in various formats like PNG, PDF, SVG, and more. Basic Steps To save a figure as a file from iPython, we can take the following steps − Create a new figure or activate an existing figure Add an axes to the figure using add_axes() method Plot the data Save the plot using savefig() method Example Here's ...
Read MoreMatplotlib – Plot over an image background in Python
To plot over an image background in Matplotlib, you can overlay plots on top of images using imshow() and standard plotting functions. This technique is useful for data visualization on maps, annotating images, or creating custom backgrounds for plots. Steps to Plot Over an Image Read an image from a file into an array using plt.imread() Create a figure and subplot with plt.subplots() Display the image using imshow() with specified extent Create your plot data and overlay it using standard plotting methods Display the final result with plt.show() Example Here's how to create a ...
Read MoreHow to specify values on Y-axis in Python Matplotlib?
In Matplotlib, you can customize the Y-axis values using the yticks() method to specify both the positions and labels of the ticks. This is useful when you want to replace numeric values with descriptive labels or control exactly which values appear on the axis. Basic Y-axis Customization The yticks() method allows you to specify tick positions and their corresponding labels − import numpy as np import matplotlib.pyplot as plt # Create sample data x = np.array([0, 2, 4, 6]) y = np.array([1, 3, 5, 7]) # Create custom labels for both axes ticks = ...
Read MoreMatplotlib – How to insert a degree symbol into a Python plot?
To insert a degree symbol into a Python plot, you can use LaTeX representation with the syntax $^\circ$. This is particularly useful when plotting temperature data or angular measurements. Steps Create data points for pV, nR and T using numpy Plot pV and T using plot() method Set xlabel for pV using xlabel() method Set the label for temperature with degree symbol using ylabel() method To display the figure, use show() method Example Let's create a simple temperature vs ...
Read MoreHow to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y?
When plotting with Pandas using secondary_y, grid lines are enabled by default. You can remove them by setting grid=False in the plot method. Creating a DataFrame with Sample Data First, let's create a DataFrame with two columns of data ? import pandas as pd import matplotlib.pyplot as plt # Set figure parameters plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create sample data data = pd.DataFrame({ "column1": [4, 6, 7, 1, 8], "column2": [1, 5, 7, 8, 1] }) print(data) ...
Read MoreHow to get the list of axes for a figure in Pyplot?
In Matplotlib, you can retrieve all axes objects from a figure using the get_axes() method. This returns a list containing all axes in the figure, which is useful for programmatically manipulating multiple subplots. Basic Example with Single Axes Let's start with a simple example that creates a figure with one subplot and retrieves its axes ? import numpy as np import matplotlib.pyplot as plt # Create data xs = np.linspace(1, 10, 10) ys = np.tan(xs) # Create figure and add subplot fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(111) # Get axes using get_axes() ...
Read MoreCircular (polar) histogram in Python
A circular (polar) histogram displays data in a circular format using polar coordinates. This visualization is particularly useful for directional data like wind directions, angles, or cyclic patterns. Creating a Basic Polar Histogram To create a polar histogram, we need three main components: angles (theta), radii (heights), and bar widths. Here's how to create one ? import numpy as np import matplotlib.pyplot as plt # Set up data N = 20 theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) radii = 10 * np.random.rand(N) width = np.pi / 4 * np.random.rand(N) # Create polar ...
Read MoreHow to add different graphs (as an inset) in another Python graph?
To add different graphs (as an inset) in another Python graph, we can use Matplotlib's add_axes() method to create a smaller subplot within the main plot. This technique is useful for showing detailed views or related data alongside the primary visualization. Steps to Create an Inset Graph Create x and y data points using NumPy Using subplots() method, create a figure and a set of subplots Add a new axis to the existing figure using add_axes() Plot data on both the main axis and the inset axis Use show() method to display the figure Basic ...
Read MoreHow to put text outside Python plots?
To put text outside a Python plot, you can control the text position by adjusting coordinates and using the transform parameter. The transform parameter determines the coordinate system used for positioning the text. Steps Create data points for x and y Initialize the text position coordinates Plot the data using plot() method Use text() method with transform=plt.gcf().transFigure to position text outside the plot area Display the figure using show() method Example Here's how to place text outside the plot ...
Read MoreHow to plot a confusion matrix with string axis rather than integer in Python?
A confusion matrix with string labels provides better readability than numeric indices. In Python, we can create such matrices using matplotlib and sklearn.metrics by customizing the axis labels with descriptive strings. Basic Setup First, let's import the required libraries and prepare sample data − import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix import numpy as np # Sample data - actual vs predicted classifications actual = ['business', 'health', 'business', 'tech', 'health', 'tech'] predicted = ['business', 'business', 'business', 'tech', 'health', 'business'] # Define string labels labels = ['business', 'health', 'tech'] print("Actual:", actual) print("Predicted:", predicted) print("Labels:", ...
Read More