The plot()
method in the Jplotlib.plot()
allows you to create 2D line plots with ease. This method is designed to visualize datasets using y-coordinates, and it offers two convenient ways to generate plots:
-
plot(double[] yPoints)
:- Description: Plots a 2D line graph using the provided y-coordinates.
- Default x-values: The x-coordinates start from 0 and increment by 1 for each point.
- Example usage:
new Jplotlib.plot(new double[]{2.5, 5.1, 3.9, 6.2});
-
plot(double[] xPoints, double[] yPoints)
:- Description: Plots a 2D line graph using the given x-coordinates and y-coordinates.
- Example usage:
double[] xCoords = {1.0, 2.0, 3.0, 4.0}; double[] yCoords = {2.5, 5.1, 3.9, 6.2}; new Jplotlib.plot(xCoords, yCoords);
To customize the color of the line in the plot, you can use the .color()
method available in the Jplotlib library. This method allows you to specify the color using either the LibColor
enum from Jplotlib or the java.awt.Color
class.
The LibColor
enum provides a set of predefined colors that you can use to style the line in your plot. Here's an example of how to use it:
import io.github.manishdait.jplotlib.Jplotlib;
import io.github.manishdait.jplotlib.defaults.color.LibColor;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
jplotlib.plot(y1)
.color(LibColor.LIME.getColor());
jplotlib.show();
}
}
In this example, we use the .color(LibColor.LIME.getColor())
method to set the color of the line to lime.
For more information about the LibColor
enum and the available colors, refer to the LibColor Enum section.
If you prefer to use the java.awt.Color
class, you can do so as follows:
import java.awt.Color;
import io.github.manishdait.jplotlib.Jplotlib;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
jplotlib.plot(y1)
.color(Color.RED);
jplotlib.show();
}
}
In this example, we use the .color(Color.RED)
method to set the color of the line to red.
Whichever method you choose, the .color()
method allows you to customize the appearance of your line plots with different colors according to your preferences.
To change the width of the line in the plot, you can use the .lineWidth()
method available in the Jplotlibplot()
.
import io.github.manishdait.jplotlib.Jplotlib;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
jplotlib.plot(y1)
.lineWidth(15F);
jplotlib.show();
}
}
In this example, we use the .lineWidth(15F)
method to set the width of the line to 15.
The value passed to .lineWidth()
should be a floating-point number between 1 and 15. A smaller value makes the line thinner, while a larger value makes it thicker. For example, using .linewidth(1F)
will create a thin line, whereas .linewidth(15F)
will create a thicker line.
To change the style of the plotted line in the Jplotlib.plot()
method, you can use the .lineStyle()
method. This method allows you to customize the appearance of the line using the Stoke
enum from Jplotlib, which currently contains two possible values: NONE
and DASHED
.
import io.github.manishdait.jplotlib.Jplotlib;
import io.github.manishdait.jplotlib.defaults.line.Stroke;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
jplotlib.plot(y1)
.lineStyle(Stroke.DASHED);
jplotlib.show();
}
}
In this example, we use the .lineStyle(Stroke.DASHED)
method to set the style of the line to dashed.
For more information about the Stroke
enum and the available styles, refer to the Stroke Enum section.
To add markers to line points in Jplotlib.plot()
, you can use the .marker()
method. This method allows you to customize the appearance of the markers using the Marker
enum from Jplotlib, which currently supports two marker types: NONE
, CIRCLE
, and SQUARE
.
import io.github.manishdait.jplotlib.Jplotlib;
import io.github.manishdait.jplotlib.style.marker.Marker;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
jplotlib.plot(y1)
.marker(Marker.CIRCLE);
jplotlib.show();
}
}
In this example, we use the .marker(Marker.CIRCLE)
method to add circular markers to the line points.
For more information about the Marker
and how to use, refer to the Marker section.
You can plot as many lines as you like by simply adding more Jplotlib.plot()
method calls:
import io.github.manishdait.jplotlib.Jplotlib;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] y1 = {6, 2, 7, 11};
double[] y2 = {3, 8, 1, 10};
jplotlib.plot(y1);
jplotlib.plot(y2);
jplotlib.show();
}
}
In this example, we create two sets of y-coordinates (y1
and y2
) and plot them using two separate Jplotlib.plot()
method calls. We customize the appearance of each line using different line styles and markers.
You can extend this approach to plot any number of lines by adding more Jplotlib.plot()
method calls with their respective datasets and customizations.
You can also plot multiple lines by specifying both the x
and y
coordinates for each line in the same plt.plot()
function. The x and y values should come in pairs:
(In the examples above we only specified the points on the y-axis, meaning that the points on the x-axis got the the default values (0, 1, 2, 3).)
The x- and y- values come in pairs:
import io.github.manishdait.jplotlib.Jplotlib;
public class App {
public static void main(String[] args) {
Jplotlib jplotlib = new Jplotlib();
double[] x1 = {0, 1, 2, 3};
double[] x2 = {2, 3, 4, 5};
double[] y1 = {6, 2, 7, 11};
double[] y2 = {3, 8, 1, 10};
jplotlib.plot(x1, y1);
jplotlib.plot(x2, y2);
jplotlib.show();
}
}
In this example, we create two sets of y-coordinates (y1
and y2
) and two sets of x-coordinates (x1
and x2
) and plot them using two separate Jplotlib.plot()
method calls.