Julia - Working with Matplotlib's Pyplot Class
Last Updated :
26 Apr, 2023
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");
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:
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:
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:
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:
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:
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:
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:
Similar Reads
Matplotlib.pyplot.axis() in Python axis() function in Matplotlib is used to get or set properties of the x- and y-axis in a plot. It provides control over axis limits, aspect ratio and visibility, allowing customization of the plotâs coordinate system and view. It's key feature includes:Gets or sets the axis limits [xmin, xmax, ymin,
3 min read
Matplotlib.pyplot.hlines() in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Matplotlib.pyplot.hlines() The Matplotlib.pyplot.hlines() is used to draw horizontal lin
2 min read
Matplotlib.pyplot.axvspan() in Python Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.\ Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-s
2 min read
Matplotlib Pyplot API Data visualization plays a key role in data science and analysis. It enables us to grasp datasets by representing them. Matplotlib, a known Python library offers a range of tools, for generating informative and visually appealing plots and charts. One outstanding feature of Matplotlib is its user-ve
4 min read
Matplotlib.pyplot.quiver() in Python Matplotlib is a library of Python bindings which provides the user with a MATLAB-like plotting framework. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Matplotlib.pyplot.qui
2 min read
Matplotlib.pyplot.plot() function in Python The matplotlib.pyplot.plot() is used to create 2D plots such as line graphs and scatter plots. The plot() function allows us to plot data points, customize line styles, markers and colors making it useful for various types of visualizations. In this article, we'll see how to use this function to plo
3 min read
Matplotlib.pyplot.locator_params() in Python Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays.Pyplot is a collection of command style functions that make matplotlib work like MATLAB. Note: For more information, refer to Python Matplotlib â
2 min read
Matplotlib.pyplot.plot_date() function in Python Matplotlib is a module package or library in Python which is used for data visualization. Pyplot is an interface to a Matplotlib module that provides a MATLAB-like interface. The matplotlib.pyplot.plot_date() function is like the regular plot() function, but it's tailored for showing data over dates
3 min read
Simple Plot in Python using Matplotlib Creating simple plots is a common step in data visualization. These visual representations help us to understand trends, patterns and relationships within data. Matplotlib is one of the most popular plotting libraries in Python which makes it easy to generate high-quality graphs with just a few line
4 min read