We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
Plotting and Data Visualization
A. Basic 2D plotting: plot function, customizing titles, labels, legends.
1. Introduction to the plot Function The plot function in Scilab is used for creating basic 2D line plots. You can use it to plot a function or data points by providing x and y coordinates. Example code: // Define x and y data points x = 0:0.1:10; y = sin(x); // Plot the data plot(x, y); This will plot the sine function from 0 to 10. 2. Customizing Titles, Labels, and Legends a. Adding a Title To add a title to your plot, use the title function. Example code: // Adding a title to the plot plot(x, y); title("Sine Wave Plot"); b. Labeling Axes To add labels to the x-axis and y-axis, use xlabel and ylabel. Example code: // Adding x and y labels plot(x, y); xlabel("X Values"); ylabel("sin(X)"); c. Adding Legends To add a legend to the plot, use the legend function. Example code: // Adding a legend to the plot plot(x, y); legend("Sine Function"); 3. Complete Example with Customizations Here's a full example that combines all these customizations. Code: // Define data x = 0:0.1:10; y1 = sin(x); y2 = cos(x);
// Plot the data with customization
plot(x, y1, "b-"); // Blue line for sine plot(x, y2, "r--"); // Red dashed line for cosine title("Sine and Cosine Functions"); xlabel("X Values"); ylabel("Function Values"); legend("Sine", "Cosine"); B. Plotting multiple functions and using subplots. // Define the x range for plotting x = linspace(-10, 10, 100);
// Create a 2x2 grid for subplots
// Plot 1: Sine function subplot(2, 2, 1); // Position 1 in a 2x2 grid plot(x, sin(x)); title("y = sin(x)");
// Plot 2: Cosine function
subplot(2, 2, 2); // Position 2 in a 2x2 grid plot(x, cos(x)); title("y = cos(x)");
// Plot 3: Exponential function
subplot(2, 2, 3); // Position 3 in a 2x2 grid plot(x, exp(x)); title("y = exp(x)");
// Plot 4: Hyperbolic Tangent function
subplot(2, 2, 4); // Position 4 in a 2x2 grid plot(x, tanh(x)); title("y = tanh(x)"); C. Introduction to simple 3D plotting: plot3d and mesh. // Define the grid of points for the x and y axes x = linspace(-10, 10, 50); // 50 points between -10 and 10 y = linspace(-10, 10, 50); // 50 points between -10 and 10
// Create a meshgrid for x and y
[X, Y] = meshgrid(x, y);
// Define a function z = f(x, y)
Z = sin(sqrt(X.^2 + Y.^2)) ./ sqrt(X.^2 + Y.^2);
// Plotting using plot3d
clf(); subplot(1, 2, 1); // Divide the plot window into a 1x2 grid and select the first part plot3d(x, y, Z); title("3D Plot using plot3d"); xlabel("X-axis"); ylabel("Y-axis"); zlabel("Z-axis");
// Plotting using mesh
subplot(1, 2, 2); // Select the second part of the plot window mesh(x, y, Z); title("3D Mesh Plot"); xlabel("X-axis"); ylabel("Y-axis"); zlabel("Z-axis");
// Show both plots
show_window(); D. Example: Plotting sine and cosine waves with customized properties. x = 0:0.1:2*%pi; y1 = sin(x); y2 = cos(x); clf(); plot(x, y1, 'r'); e = gce(); e.thickness = 2; plot(x, y2, 'g--'); e = gce(); e.thickness = 2;