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

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads