Learn PythonPythonPython GUI

What Are The Top 5 Graph Tools For Python

cover

Among the strongest features of Python is its endless capabilities of presenting data visually made possible by simple-to-use graph tools. The traditional matplotlib package is usually the first for the beginner Python programmer to learn. Matplotlib is also employed by the pandas package which is the de-facto tool used in data science. Another package called seaborn takes the plotting capabilities of matplotlib to the next level. They look particularly good when used in Python notebooks. There are alternative graph tools for Python, such as the Plotly, Bokeh and Altair packages.

Let’s start by downloading PyScripter – a great GUI for Python coding.

Why use Python graph tools?

Python is the programming language of choice for data science and machine learning. Data often comes from noisy sources, there are missing or incorrect values. In other cases, it is necessary to have an initial overview of the data before proceeding with its further processing. Data visualization is an essential first step in choosing appropriate methods of numerical analysis.

What kind of plots can one create using Python graph tools?

  • Line plot
  • Scatter plot
  • Histogram
  • Pie chart
  • Heat map
  • Contour plot
  • 3D plot
  • Animations

What is the most common Python graph tool?

Matplotlib is the traditional plotting package used in hundreds of thousands of Python packages.

Getting started

I shall start with the very basics. We need two lists of points – for the x- and the y-axis of a scatter plot.

What Are The Top 5 Graph Tools For Python - a straight line plot

Plotting functions using the matplotlib graph tool for Python and NumPy

Let’s create a simple data set using the NumPy package and plot simple mathematical functions. We need a set of equally spaced points for the x-axis. This can be done using the arange function from NumPy. On the y-axis, we will plot some lines and curves. By the way – that IS the correct spelling of arange – think of it as “a range” not “arrange”.

I will define lambda functions. They are often considered advanced Python concepts, but they actually are very simple. A lambda function is a concise way of writing a function on one line. For example, I define the function multiply which takes two numbers a and b, and returns their product, a*b. Then I set the Y values that we want to plot by calling the multiply function and giving as parameters the list of points on the x-axis and a number.

I will define two lines, one for which every value of y is the same as x, and another one where y is twice as big as x.

What Are The Top 5 Graph Tools For Python - two straight line plots

We can plot curves, as well. A simple parabola is defined by x2, so I can again use the multiply function to create the points.

What Are The Top 5 Graph Tools For Python - a curved line plot

How to change colours and line styles

Python automatically assigns different colours to lines, however, sometimes it is useful to change them. This can be done by specifying the name of the colour. A full list of colours is available online by searching for CSS colours.

Lines can be plotted in different styles such as dotted or dashed. This is set by the parameter linestyle.

What Are The Top 5 Graph Tools For Python - a curved line plot showing different line types

There is a shorter way of defining line type and/or colour. For example, a red dashed line is defined by

A blue dotted line can be made using

And a green dot-dash line is simply

The points can be individually drawn as markers. This is similar to the syntax above. There are many marker styles – points, squares, triangles and more.

What Are The Top 5 Graph Tools For Python - curved line plots with symbols for the data points

Line thickness can be adjusted using the linewidth property.

What Are The Top 5 Graph Tools For Python a curved line plot with differing line thicknesses

How to set labels?

What Are The Top 5 Graph Tools For Python - three curved lines from a function

How to best plot large amounts of data?

Large amounts of data are usually processed using the pandas package. It utilises matplotlib to make some basic plots of the data, including line plots, histograms, and bar graphs. Advanced examples are available in the documentation of pandas.

I have downloaded a small part of the famous iris data set in CSV format. It contains various data about a large number of properties of three species of iris flowers, including sepal length and width and petal length and petal width.

Loading data with pandas

The code below loads the data about the iris flowers into a data frame. It automatically assigns the names of the columns as given in the CSV file. Pandas employs the plotting methods from matplotlib to easily create graphs. I will plot the sepal length and width as a histogram.

The output looks like this:

What Are The Top 5 Graph Tools For Python - side by side bar graphs of different sepal lengths

Pandas is rich in features and it would take several more articles to go into all possible details. More information and examples can be found in the documentation of pandas.

How to plot multiple plots at once?

While matplotlib is the traditional Python graph tool, but there are plenty of parameters to set. Pandas significantly simplifies the process.

What Are The Top 5 Graph Tools For Python - different plots at the same time

How to make plots even more pleasing?

The pandas graphs are quite okay and tell clearly the distribution of the data, however, there is room for improvement. The seaborn package brings in significant improvements.

The data distribution can be visualised as a histogram using the following code.

What Are The Top 5 Graph Tools For Python a hybrid graph of bars and a line

A more detailed plot of the correlation between the data is also very simple to do.

What Are The Top 5 Graph Tools For Python - multiple plot types

What alternatives are there to matplotlib?

What is Plotly like?

Plotly was originally written for Javascript. It can make graphs, as well as interactive plots. It has an amazing set of features, including plotting data on geographical maps. The code below creates a local server and opens the view in the browser where the user can rotate the globe and hover with the mouse for more details. This Python graph tool is very handy, and it is not necessary to know any Javascript at all.

What Are The Top 5 Graph Tools For Python a map of the world

What are the benefits of Bokeh?

Bokeh is a professional tool for advanced users. It also produces interactive plots. There is a detailed tutorial on their webpage, creating Jupyter notebooks to test out. It is definitely worth trying.

How about Altair?

Altair is user-friendly, yet very powerful. Below I am using again the iris data set.

Internally, Altair prepares a json-style string that defines the plot. It can save the output as an HTML file with the graph embedded.

What Are The Top 5 Graph Tools For Python - lots of bars on a chart

Conclusion

There are many graph tools for Python. They offer a wide variety of tools and settings for the plots. It is mostly up to personal preference which one you will like best. It is excellent for the beginner Python coder.

Check out these five graph tools in PyScripter today.

close
About author

I am a quantum chemist and a photographer with a decade of experience in programming in various languages. I have worked with python in the past five years regularly on multiple projects and in my free time. I love sharing knowledge with the world and clearly explaining complex concepts.
Related posts
Python GUI

Is Python a Scripting Language or a Programming Language?

Python

Embrace The Power Of Brand New Python 3.11 Features

Python

How To Exit A Python Script

CodeLearn PythonPythonWindows

How To Add Python Testing Tools Into Pandas Machine Learning Code

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.

it_ITItalian