Open In App

Julia – Working with Matplotlib’s Pyplot Class

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Data visualization is the process of representing the available data diagrammatically. There are packages that can be installed to visualize the data in Julia. Here, PyPlot class of the Matplotlib Module in Julia will be used for plotting.

To install the PyPlot package in Julia, the Matplotlib package of Python must be installed on the user’s device. Using the Pkg command we will install PyPlot in Julia Command-Line. Open the Julia terminal and type the following command:

using Pkg;

Pkg.add(“PyPlot”);

PyPlot Installation

 

Line Chart

Two 1-D arrays will be provided as input to the plot() function to plot a simple line chart. Below is the Julia program to plot a Line chart:

Julia




# Julia program to plot a 
# Line chart
  
# Importing the Module
using PyPlot;
  
# Defining the arrays
arr = [2,5,6,8];
arr_2 = arr*3;
  
# Plotting using plot function
plot(arr,arr_2);
  
# Providing title to the chart
title("A simple Line Chart");
  
# Displaying the chart
PyPlot.show();


Output:

Line Chart

 

The graphs can be stylized using various attributes like line color, line width, line style, labels, etc. Some of the attributes are explained below:

Julia




# Julia program to stylize line chart
# by changing line width, line color, 
# line style, labels
using PyPlot;
  
arr = [2, 5, 6, 8];
arr_2 = arr * 3;
  
plot(arr, arr_2, linewidth = 5.0,
     linestyle = "--", color = "green");
  
title("A simple Line Chart");
xlabel("This is X-Axis");
ylabel("This is Y-Axis");
  
PyPlot.show();


Output: 

Line Chart with changed Attributes

 

Attributes:

  • linewidth: It defines the width of the line. It only accepts float values.
  • linestyle: This is used to define the style of the line we are plotting, there are different styles like ‘:’, ‘-.’, ‘–‘ etc.
  • color: It is used to define the color of the line we are plotting.
  • labels: This is used to define the labels for the x-axis and y-axis of the plot.

Bar Chart

A simple bar graph can be used to compare two things or represent changes over time or represent the relationship between two items. To plot a bar chart we will use the bar() function and pass the variables/values we want to plot. Below is the Julia program to plot a bar chart:

Julia




# Julia program to plot a 
# bar chart
# Importing module
using PyPlot;
  
# Defining the values
arr = range(start = 0, stop = 50, step = 2);
arr_2 = arr * 3;
  
# Plotting the bar chart
bar(arr, arr_2, color = "red");
  
# Labels for the axes
title("A simple Bar Chart");
xlabel("This is X-Axis");
ylabel("This is Y-Axis");
  
PyPlot.show();


Output:

Bar Chart

 

Explanation: Here as we can see, inside the bar() function alongside the two arrays we have also passed a keyworded argument color which is used to define the color of the bars of the bar chart. 

Scatter Plot

It is used to reveal a pattern in the plotted data. To plot a scatter plot using the scatter() function. Below is the Julia program to plot a scatter plot:

Julia




# Julia program to plot a 
# scatter plot
  
# Importing Module
using PyPlot;
  
# Defining values for the axes
x = [5, 7, 8, 7, 2, 17, 2, 9
     4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86
     103, 87, 94, 78, 77, 85, 86]
  
# Plotting scatter plot
scatter(x, y, color = "red");
  
# Setting title for the scatter plot
# and labels for the axes
title("A simple Scatter Plot");
xlabel("This is X-Axis");
ylabel("This is Y-Axis");
  
PyPlot.show();


Output:

Scatter Plot

 

Explanation: Here also, inside of the scatter() function, the variables are passed alongside another argument color which is used to define the color of each dot of the scatter plot.

Pie Chart

A pie chart is a statistical graph used to represent proportions. The pie() function will be used to plot a Pie Chart in Julia. Below is the Julia program to plot a pie chart:

Julia




# Julia program to plot a 
# pie chart
# Importing Module
using PyPlot;
  
# Defining values for 
# pie chart
x = [2, 4, 3, 1, 7, 9, 3, 1, 4
     5, 6, 8, 10, 12, 4, 3, 9]
y = [99, 86, 87, 88, 111, 86, 103,
     87, 94, 78, 77, 85, 86
  
# Plotting pie chart
pie(x);
  
# Setting title for the Pie Chart 
title("Pie Chart using PyPlot in Julia");
  
PyPlot.show();


Output:

Pie Chart

 

Below is the Julia program to add labels and legends in the pie chart:

Julia




# Julia program to add labels
# and legends in pie chart
# Importing Modules
using PyPlot;
  
fees = [5000, 7500, 3500, 4000, 6500]
gfg = ["DSA", "System Design", "Machine Learning"
       "Data Science", "Blockchain"]
  
# Setting labels
pie(fees, labels = gfg);
  
# Setting legends
PyPlot.legend(bbox_to_anchor = (1.50, 1.00), loc = "upper right");
  
# Setting title for Pie Chart
title("Pie Chart using PyPlot in Julia");
  
PyPlot.show();


Output: 

Pie Chart with labels and legends

 

Attributes:

  • bbox_to_anchor: Here a keyworded argument bbox_to_anchor has been used to position the Legend properly so that it doesn’t overlap the plot. It is available in the matplotlib library of Python and can be used here too. 
  • loc: This is used to mention the location of the legend we are showing with our Pie Chart.

Histogram

Histograms are mostly used for displaying the frequency of data. The bars are called bins. Taller the bins more the data falls under that range. The hist() function will be used to plot a Histogram. Below is the Julia program to plot a Histogram:

Julia




# Julia program to plot a 
# Histogram
# Importing Module
using PyPlot;
  
x = [2, 4, 3, 1, 7, 9, 3, 1,
     4, 5, 6, 8, 10, 12, 4, 3, 9]
  
# Plotting a Histogram
hist(x);
  
# Setting title
title("Histogram using PyPlot in Julia");
  
# Setting label for the axes
xlabel("This is X-Axis");
ylabel("This is Y-Axis");
  
PyPlot.show();


Output:

Histogram

 



Similar Reads

Julia end Keyword | Marking end of blocks in Julia
Keywords in Julia are reserved words that have a pre-defined meaning to the compiler. These keywords can't be used as a variable name. 'end' keyword in Julia is used to mark the end of a block of statements. This block can be of any type like struct, loop, conditional statement, module, etc. Syntax: block_type block_name Statement Statement end Exa
1 min read
Julia function keyword | Create user-defined functions in Julia
Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can't be used as variable names. 'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the c
1 min read
Julia continue Keyword | Continue iterating to next value of a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can’t be used to name variables. 'continue' keyword in Julia skips the statement immediately after the continue statement. Whenever the continue keyword is executed, the compiler immediately stops iterating over further values and sends the execution
1 min read
Julia break Keyword | Exiting from a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can't be used to name variables. 'break' keyword in Julia is used to exit from a loop immediately. Whenever the break keyword is executed, the compiler immediately stops iterating over further values and sends the execution pointer out of the loop. S
1 min read
Julia local Keyword | Creating a local variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.'local' keyword in Julia is used to create a variable of a limited scope whose value is local to the scope of the block in which it is defined. Synt
2 min read
Julia global Keyword | Creating a global variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution. 'global' keyword in Julia is used to access a variable that is defined in the global scope. It makes the variable where it is used as its current s
2 min read
Working with DataFrames in Julia
A Data frame is a two-dimensional data structure that resembles a table, where the columns represent variables and rows contain values for those variables. It is mutable and can hold various data types. Julia is a high performance, dynamic programming language which has a high-level syntax. The DataFrame package in Julia gives an ability to create
7 min read
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you don’t want to prefix on each function just add using Dates in your code and then you will be able t
5 min read
Working with Text Files in Julia
Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. Julia supports file handling like other programming
4 min read
Working with Excel Files in Julia
Julia is a high-level open-source programming language meaning that its source is freely available. It is a language that is used to perform operations in scientific computing. Julia is used for statistical computations and data analysis. Julia provides its users with some pre-defined functions and built-in packages with the help of which Julia mak
7 min read
Article Tags :