DEV Community

Cover image for Converting the graphs into scientific styles
Mystique Lord
Mystique Lord

Posted on

Converting the graphs into scientific styles

Hi everyone! In this article, we consider the SciencePlots library that allows us convert simple graphs into a scientific format. In this format, the graphs are displayed in some types of scientific articles.

Initial actions

Installing

Before using the SciencePlots library, we need to install it with the help of the commands according to your operating system.
Windows:

pip install SciencePlots
Enter fullscreen mode Exit fullscreen mode

MacOS:

pip3 install SciencePlots
Enter fullscreen mode Exit fullscreen mode

Linux:

pip install SciencePlots
Enter fullscreen mode Exit fullscreen mode

Also, we need to install the matplotlib library to create graphs. Commands to install the library are available below.
Windows:

pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

MacOS:

pip3 install matplotlib
Enter fullscreen mode Exit fullscreen mode

Linux:

pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

Preparing the data and writing the code

Import the libraries:

import scienceplots
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

As data, we take our information about Bitcoin’s worth in 2025:

bitcoin_prices = [102405.03, 84373.01, 82548.91, 94207.31, 104638.09, 107135.34, 115758.20, 108236.71, 114056.08,                      109556.16, 90394.31, 87263.99]

bitcoin_prices_labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
Enter fullscreen mode Exit fullscreen mode

The next step is to create our graph:

plt.plot(bitcoin_prices_labels, bitcoin_prices)
plt.title("Bitcoin prices in 2025")

plt.xlabel("Months")
plt.ylabel("Real prices ($)")
plt.show()
Enter fullscreen mode Exit fullscreen mode

Here’s the full code; however, it will be the function called simple_graph():

import scienceplots
import matplotlib.pyplot as plt


def simple_graph():
    bitcoin_prices = [102405.03, 84373.01, 82548.91, 94207.31, 104638.09, 107135.34, 115758.20, 108236.71, 114056.08,
                      109556.16, 90394.31, 87263.99]

    bitcoin_prices_labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",  "Aug", "Sep", "Oct", "Nov", "Dec"]

    plt.plot(bitcoin_prices_labels, bitcoin_prices)
    plt.title("Bitcoin prices in 2025")

    plt.xlabel("Months")
    plt.ylabel("Real prices ($)")
    plt.show()


simple_graph()
Enter fullscreen mode Exit fullscreen mode

Now run the code in a way that we can see the graph without any styles:

Base styles

Science style

So, firstly, we consider the science style, but without a LaTeX font. To set the style, we need to define the style as the main style of our graph. We can do it with the help of the style package and use the use() function.
Here’s the code example, where we set the styles:

plt.style.use(["science", "no-latex"])
Enter fullscreen mode Exit fullscreen mode

In the use() function, we define one or more styles, which are sets of visual attributes for graph elements. If we combine multiple styles in a single graph, we need to specify them as a list.
So, our current code looks like this:

import scienceplots
import matplotlib.pyplot as plt


def simple_graph():
    plt.style.use(["science", "no-latex"])

    bitcoin_prices = [102405.03, 84373.01, 82548.91, 94207.31, 104638.09, 107135.34, 115758.20, 108236.71, 114056.08, 109556.16, 90394.31, 87263.99]

    bitcoin_prices_labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

    plt.plot(bitcoin_prices_labels, bitcoin_prices)
    plt.title("Bitcoin prices in 2025")

    plt.xlabel("Months")
    plt.ylabel("Real prices ($)")
    plt.show()


simple_graph()
Enter fullscreen mode Exit fullscreen mode

Run the code:

IEEE style

The style makes the graph only with black and white colours.
Set the style:

plt.style.use(["ieee", "no-latex"])
Enter fullscreen mode Exit fullscreen mode

Also, we should make one crucial change when we use the IEEE style. The style didn’t work correctly only with the common show() function, and that’s why we need to write the code:

plt.rcParams.update(
        {"figure.dpi": "100"}
)
Enter fullscreen mode Exit fullscreen mode

The code helps us to correctly set the graph settings. Without these settings, we can get the graph in an inappropriate, enormous size. Overall, the IEEE format was developed specifically for scientific magazines, and that’s the reason why the graphs are created in a tiny size. The development environment tries to resize the graphs, and as a result, we got enormous graphs.
But if we define the DPI as 100 (Dots Per Inch), the graph will convert into a larger quantity of pixels, which makes the image’s quality much better.
So, in the result, we’ll get the graph:

Notebook style

The style is used by the Jupyter Notebook.
Set the style:

plt.style.use(["notebook"])
Enter fullscreen mode Exit fullscreen mode

We’ll get such a format:

Nature style

The style is used by the Nature scientific magazine.
Set the style in code:

plt.style.use(["nature"])
Enter fullscreen mode Exit fullscreen mode

That’s the result:

Grid style

The grid style adds the grid to our graph.
Set the style in code:

plt.style.use(["grid"])
Enter fullscreen mode Exit fullscreen mode

That’s why we get this graph:

Style mixing

Science + no-latex + grid styles

An example of how to mix different styles, such as science and no-latex, with a grid, can be seen below:

plt.style.use(["science", "no-latex", "grid"])
Enter fullscreen mode Exit fullscreen mode

As we can see, we just changed only one row and added the grid style to another two.
Our graph will look like this:

Science + no-latex + notebook styles

Just add the third style in code:

plt.style.use(["science", "no-latex", "notebook"])
Enter fullscreen mode Exit fullscreen mode

So, we’ll see the result:

Science + no-latex + nature styles

Change the styles in code:

plt.style.use(["science", "no-latex", "nature"])
Enter fullscreen mode Exit fullscreen mode

The result:

Science + no-latex + ieee styles

Add the IEEE style to our current ones:

plt.style.use(["science", "no-latex", "ieee"])
Enter fullscreen mode Exit fullscreen mode

In the next step, we need to clearly define the font that our graph style will use. The problem is that the IEEE style uses only Times New Roman font, but the program will search for the font called Times, which isn’t the full name of the font. We’ve got an error because we don’t have a font with the same name.
To exit from the situation, we need to clearly define that we will search for Times New Roman instead of Times.
That’s why we define the font:

plt.rcParams.update(
        {
            "font.family": "serif",
            "font.serif": "Times New Roman",
            "figure.dpi": "100",
        }
    )
Enter fullscreen mode Exit fullscreen mode

Run the code:

Conclusion

In this article, we learned how to change and add some specific styles for the matplotlib graphs. So, now we know why we need to use certain styles and how to use them in code. Thanks for your attention!

Used resources

(n. d.). Bitcoin USD ( BTC-USD) — Price History. Digrin. https://www.digrin.com/stocks/detail/BTC-USD/price

Top comments (0)