Logarithmic axis

Is there an easy way to change the y-axis from linear to logarithmic?

use std::io;
use plotters::prelude::*;
use std::process::Command;

fn collatz(mut n: i128) -> Vec<i128> {
    let mut sequence = vec![n];

    while n != 1 {
        if n % 2 == 0 {
            n = n / 2;
        } else {
            n = 3 * n + 1;


fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Enter an integer for the Collatz sequence:");

    let mut input_value = String::new();
    io::stdin().read_line(&mut input_value)?;

    let input_value = match input_value.trim().parse::<i128>() {
        Ok(value) if value > 0 => value,
        _ => {
            println!("Invalid input. Please provide a valid positive integer.");
            return Ok(());

    let sequence = collatz(input_value);

    // Create a plot
    let root = BitMapBackend::new("collatz_sequence.png", (800, 600)).into_drawing_area();

    let max_value = *sequence.iter().max().unwrap() as i128;

    let mut chart = ChartBuilder::on(&root)
        .caption("Collatz Sequence", ("sans-serif", 40))
        .build_cartesian_2d(0..sequence.len() as i128, 0..max_value)?;

    // Plot the Collatz sequence

        sequence.iter().enumerate().map(|(i, &val)| (i as i128, val)),

    // Finish the plot to ensure it's saved

    // Open the generated image using the 'open' command
        .expect("Failed to open image");


From a quick glance at the documentation it seems you need to import the plotters::coord::combinators::IntoLogRange and then change the line with buile_cartesian_2d into:

.build_cartesian_2d(0..sequence.len() as i128, (0..max_value).log_scale())?;

This should make the y axis log scale.

Thank you!

That worked ou fine. However I had to replace i128 with i64 in the code because i128 seems not to be supported.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.