Categories
PROGRAMMING PYTHON TOOLS TUTORIALS

Difference between append and extend list methods in Python

In this article, we will learn the differences between append and extend list methods in Python. Each of these methods have their own advantages and disadvantages. So we will take a look at each of these methods and see when to use which.

Sounds good? Great! Then let us get started.

Difference between append and extend list methods in Python
Difference between append and extend list methods in Python

To begin with, let me tell you the environment I am using to run my Python code. I am using Python 3 installation running on a Ubuntu 16.04 OS. But even if you have a different set up, I think this code should still run fine for you as well.

Alright, so with that out of the way, let us start looking at the actual code itself.

So to begin with, let us enter into the Python interpreter using the command:

Python 3
Python 3.6.9 (default, Jul  3 2019, 15:36:16) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

So with this, we are now in our Python interpreter. Which means, every instruction that I now type in the console above will be taken in as a Python command or input. Right?

So then, let us create two new Python lists here. We can do this by using the commands:

a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

So now we have our two Python lists. It is time for us to now see how we can use the append() and extend() list methods to work on these two lists.

Python List append( ) method

So what does this list append( ) method do? Let us find out by running the below command:

>>> a.append(b)
>>> 

So what do you think happened here? We don’t see any output after running the above command. So does that mean the code had no impact at all? Or do you think list b gets appended to list a so that we have one giant list of both a and b list elements?

Take a minute to think over before continuing further.

So to understand what happened, let us see what the value of each of these lists are. Here is the list outputs I see now:

>>> b
[6, 7, 8, 9, 10]
>>> a
[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
>>> 

So as you can see above, list b is still the same. But list a has now changed. But the content of list a is not what we expected!

So it looks like what happened here is that append( ) method simply inserted the entire list b as a new list element to list a!

Woah! Not the output we were expecting. Right? But that is what Python list’s append( ) method does. It simply adds it’s arguments as a new list element to it’s parent list!

Python List extend( ) method

So now that we know what list append( ) does, it is now time for us to see what extend( ) method will do. So let us restart our Python3 interpreter and add these two list once again:

python3
Python 3.6.9 (default, Jul  3 2019, 15:36:16) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> a = [1, 2, 3, 4, 5]
>>> b = [6, 7, 8, 9, 10]
>>> 

But this time, we will call the Python list’s extend( ) method.

>>> a.extend(b)
>>> 

Once again, it appears like nothing much changed. But we know that one of the list could be altered like in the case of append method right. So let us check what the content of list b & a is right now.

>>> b
[6, 7, 8, 9, 10]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Aha! As you can see now, list b still remains the same, but list a has the the individual elements of list b appended to it! So in other words, we have managed to “extend” list a to include content of list b.

Now this is exactly what we wanted! Right?

So with this understanding, we can now easily say what the difference between the two list functions are.

Difference between Python list append and extend methods

When we use Python’s append( ) method on a list, the parameter passed through this method will be appended as one new element to the parent list.

But on the other hand, when we use the extend( ) list method, the elements of the list passed as the parameter will be added individually to the parent list!

I think for the most use cases, we want the behavior of extend( ) method while working with lists. But there will be situations where the behavior of the append( ) list method is also preferred!

Hence, we have these two different Python List methods – append and extend!

I hope this was clear to you. But if you still have any questions or comments, do let me know in the comment section below. I would love to hear about it.

So with this, I will end my article here. Until next time, take care!

Want to know how to check for a substring using Python? Check this out!

Categories
PROGRAMMING PYTHON TUTORIALS

Python Check Substring In String – How To Guide

So do you want to know how to check the presence of a substring in a string in Python? Then this is the right article for you! In this article, I will explain you how to write simple Python code to see if a substring exists within a string. Does that sound good to you? Great then let us get started!

Python Check Substring In A String
Python Check Substring In A String

But before we take a look at the code, let us explore where we often come across this kind of requirement. Where do you think you want to check the presence of a substring within a string? Any guess?

Python Check Substring In A String – Use Case

Well, the first thing that comes to my mind is during data extraction from a data dump. Say for example, you have been given a data dump of customer emails. Your manager wants you to go through the dump and check to see how often people complain about a particular problem. Say for example, customers always complain about battery low error message that pops up on their smartphone screen reads like: “Error: Battery Low!” when using your product.

So how do you go about solving this? One crude method is to go through each of the customer emails and count the emails that talks about this error message. This will give you statistical data on that particular error. But do you think this is efficient?

No! Because you being a Python programmer should not be doing this by hand. Instead, what I would say is this:

Write a Python script to check for the substring in a string. So then you can go through each email text’s strings and check for the substring:

“Error: Battery Low!”

and count how many emails has this error message. Now that is efficient. Right?!

But this is not the only time you may want to write a Python script for substring check. Because you can use this similar concept when working with databases as well! So if you are developing a website using Flask or Django, similar code will come in handy. Say you want to fetch all customers from the DB whose name contains “Jeff” in them. You can use the same code logic even there. Right?

So, how does the code look like then?

Python Script To Check Substring In String

Here is how you can write code to check substring in a string:

import sys

def extract_strings_with(sub_string, strings_list):
    for string in strings_list:
        if sub_string in string.lower():
            print(string)

if __name__ == "__main__":
    names = ["AbsoulteDelight", "Abnormal", "Alltools", "Activator", "Alladdin", "Absoulte Vodka"]

    if len(sys.argv) < 2:
        print("Usage: substring_check.py <sub_name_to_check>\n \
                Example: substring_check.py absolute")
        sys.exit(0)

    check_subname = sys.argv[1]
    extract_strings_with(check_subname, names)

We run this Python script using the command:

python3 substring_check.py absolute

So as you can see from the above code, we are checking for the substring “absolute” present within a list of names.

So where is the magic of substring check within string happening? It is in the line:

if sub_string in string.lower():

Here, we are checking for the presence of the substring within the string element. We are using lower() function to ensure that this program is not case sensitive. But if you want it to be case sensitive, you should remove lower() function call in the above line of code.

Conclusion

So this is how you can check for the presence of a substring within a string using Python. But if you are still not clear, do let me know in the comments below. I will be more than happy to help!

So until next time, take care!

Categories
DATA SCIENCE JAVASCRIPT PYTHON TUTORIALS WEB DEVELOPMENT

Call Node.js Script From Python – How To Guide

So you have come across a situation where in you want to call a Node.js script from your Python code. But how do we go about doing that? Is there any package that we need to install in Python or Node.js that will help us with this? Or can we do it directly using basic Python and Node.js programming? What is the standard approach to solve this?

Call Node.js script from Python
Call Node.js script from Python

We will answer all the above questions in this article today. So, are you are ready to learn about it now? Cool, let us get started right away!

Setup Required To Call Node.js Script From Python

So to begin with, we will first take a look at what all needs to be installed in our computer to get this going. Alright?

Now as you know, to run any Javascript file on your computer, we need to have Node.js installed. This is the basic thing, right? So, make sure that you have installed Node.js in your computer where you want to call the Node.js script.

There are several tutorials available on the internet that can guide you in how to install Node.js on your computer. But let me give you a gist of the steps usually involved for a Linux computer:

Step 1 – Install Node Package Manager (NPM)

You can do this by running the command:

sudo apt install npm

Step 2 – Check which version of Node.js is installed

You can do this by running the command:

nodejs -v

How To Call Node.js Javascript File From Python

Now that you have your Node.js installed, we can run any Javascript file by issuing the command:

nodejs yourFile.js

That is it! It is as simple as that!

So with Node.js installation out of the way, we can now see how to call a Node.js script from a Python file.

A simple Javascript Hello World File To Run Using Node.js

To begin with, let us create a simple Node.js file that simply prints “Hello, World!” on our screen. So here is how the file looks like:

console.log("Hello, World!");

Save this as hello.js in your computer directory. To test that this file works, simply call the command:

nodejs hello.js

If the above command prints the “Hello, World!” string on your console screen, it means the script is good to run as Node.js script. Alright? So that is our basic Node.js script.

How To Call Node.js Script From Python?

Now, we want to call this hello.js script file from a Python file. But how do we go about doing that? Well, here is the process we are going to follow:

What happens under the hood

  1. We know that our application program – be it Python code or Node.js code runs on top of our Operating System (OS). Right?
  2. But how does an OS run both these programs simultaneously? Well the answer is that they both run as two separate processes in an OS.
  3. But why two separate processes, you ask? It is because it is the job of an OS to make sure both can run paralelly. And it is also the OS’s job to keep these programs separate. Meaning Python program will not be able to access, read or modify the memory of Node.js program and vice versa.
  4. So that is why OS runs them in a sandbox application process where one process is not aware or have access to other process’s resources.
  5. But then how can we now call Node.js script from Python script then? Well for that, we need to take help of the OS itself.
  6. We need to let the OS know that we intend to call Node.js script from our Python script.
  7. So to do that, we will make use of a special Python library module called “subprocess”.
  8. But how do we install this module? Well that’s the best thing about this. It is not an external module that needs to be installed separately. It already comes as part of the Python’s built-in packages.

So with this understanding, we will now write a simple Python script that calls the hello.js script written above using subprocess. Sounds good? Great! Let us check it out then!

A simple script to call Node.js script From Python

So create a file called hello_js.py and add the following code to it:

from subprocess import check_output
p = check_output(['node', '~/hello.js'])
print (p)

And that is it! With just these 3 lines of code shown above, we can call our Node.js script and print out the “Hello, World” string coming from our hello.js Javascript file! Quite simple right? But what is our Python code doing here?

Let me explain you briefly on the above Python code:

Line 1: Here we are importing a function from subprocess called check_output. We will use this function to get the console output coming in from our hello.js file.

Line 2: Here we are calling the imported check_output() and passing in a parameter list to it. This list consists of the console command line arguments we wish to execute. Now if you recall, to call our hello.js file using node, what command we used? So if you noticed, we are passing the same set of commands in this parameter list! Now it all makes sense right?

Line 3: Finally, we print the output we got from our check_output() function.

Conclusion

So this is how we can call a Node.js script from our Python code. We make use of the Python’s subprocess module and call Node.js command to run the javascript file through it. It will all become clear if you practice it further with few more variations around it. But this is the gist of how to run Node.js from Python.

So with this, I will end this article now. But if you have any questions around it, do let me know in the comment section below. I will be more than happy to help!

So until next time, take care!

Categories
COMPUTER SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Send A File To Printer For Print Using Python

In this article, we will learn how to send a file to printer for printing using Python. But you may be wondering which OS we will be working with right? Well, we will look at how to print files using Python for Windows, Mac OS as well as Linux. So we will be covering all 3 major operating systems that are out there!

Send A File To Printer
Send A File To Printer

Now that should be good for most cases right? Great! Then without further ado, let us start right away!

So how do we go here? Shall we start by taking a look at each OS? I think that is a good way to do this I guess. So let me start with Windows first. Alright?

How To Send A File To Printer In Windows?

So if you are using Windows OS, then you will need to use a special Python module called win32.

Wait a minute! What is this win32 module?

Python Win32 Module

You know, to use any hardware from an application program, you need to have OS APIs. Right? Because these APIs will give you access to the hardware on which the OS is running. But this also means that if you want to access an hardware like a printer, you need it as well!

Now the Python Win32 module is one that is used to give access to the Windows OS API! Got that? Hence the name “Win32”. Because it was written to give access to the Win32 APIs of Windows OS.

Now it all makes sense right?! So we will use just this module in Windows to access our printer. Go ahead and install win32 module on your Windows computer using the command:

pip install win32

So how will our Python code to send files to a printer on Windows look like? Take a look at it for yourself!

The Code

import win32print
p = win32print.OpenPrinter(your_printer_name)
job = win32print.StartDocPrinter(p, 1, ("Test Raw text to test the Printer's Raw text printing!", None, "RAW"))
win32print.StartPrinter(p)
win32print.WritePrinter(p, "Print Me Puhleeezzz!")
win32print.EndPagePrinter(p)

Now there is a lot going on over here But do not worry. I will explain to you what is happening here.

The first thing we need to do is to import our win32 module’s win32print library. After that we will first open the printer using the OpenPrinter API from the library. So far so good, right?

What we do next is to send a simple raw print test command. We can do that by using the StartDocPrinter API of the Win32print Python module.

Now once the raw print test is done, we get into the real crux of our problem. To print our actual text. And to do that, we will use the WritePrinter( ) API of the win32print library!

That is all there is to it for printing a file using Python code in Windows!

Now that was the case for printing in Python using a Windows computer. But how do we do that for a Mac OS or a Linux? After all these OS do not have Win32 APIs right?

Well you are right, but we don’t even need that! Wait what? Then how are we going to use the printer then? We will see how in our next section!

How To Send A File To Printer In Linux & Mac?

Fora computer running Linux or Mac (which uses POSIX APIs), calling a printer from Python is easy! Why is that?

Because in Linux & Mac, all the hardware are accessible as files! So if we want to access a printer connected to the computer, we just need to use lpr file.

So if we want to print a file called printMe.txt, we will just need to run the following code:

import os
os.system("lpr -P printer_name printMe.txt")

It is as simple as that! Now here is one more reason for you to use a Linux computer there. Right? 😉

So there you have it! This is how you can send a file to printer for printing on Windows, Mac or Linux computers. I hope this was useful for you.

But if you have any questions, do not hesitate to ask me in the comments below. I will be more than happy to help. Alright?

So until next time, have a great day!

Learn How To Get Current Timestamp In Python!

Categories
COMPUTER SCIENCE DATA SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Read Command Line Arguments In Python

In this article, we will see how to read the command line arguments passed to our Python program. So by the end of this article, you will know how to read the options sent to your program.

Sounds good? Great! Then let us start right away!

So in order for us to be able to read the parameters passed, we will make use of sys module. But before we look into this module, let us first see why want to pass arguments in the first place. Because knowing why we need it will help us understand how we can solve it later. I hope that makes sense!

What Are Command Line Arguments?

So let us start with what command line arguments are and then see how we can read them.

Say you want to write a Python program to add two numbers. Can you tell me what is the simplest code we can write for that? It looks something like this right?

a = 10
b = 20
sum = a + b
print(sum)

And when you run this piece of code, we get an output like this:

30

It is quite straight forward right? We just add the two numbers 10 & 20 and store it in the variable called sum. We then go ahead and print the value of the sum which is 30. So it is all good here. Right?

But there is one problem here. This program is quite worthless! Why? Because every time you run it, it always adds the numbers 10 & 20 to give us the result 30. Not so useful isn’t it?

Read Command Line Arguments
Read Command Line Arguments

So then what do we want? We want to write a Python program that can add two numbers. But we do not want these numbers to be fixed. Instead, we want it to take two input numbers from us and add them.

So what do we have to do for that? We will have to write a program that can take two numbers from us before it runs. So in other words, we will pass the two input values to the program as we call it to run. Now these values that we pass along with the program are called command line arguments!

Alright then! Now that we know what command line arguments are, let us learn how to read these command line arguments into our Python code.

How To Read Command Line Arguments In Python

So as we said in the earlier, reading command line arguments in Python is easy. Because we have a module called sys to do just that!

So using sys module, we can read the two input numbers from the command line. Here is the code for doing just that!

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

Okay. Before I explain what is happening here, let us see how to run this code. To run it, first we save this code in a file called sum.py. So if we want to add two numbers 1 & 2, we will run this program by passing these values as shown:

python sum.py 1 2

And when we run it, we get the following output:

3

So as you can see, the code has taken our input values 1 & 2, added them and then finally printed the result. It is doing exactly what we wanted. Right?

Understanding sys.argv

So now let me explain what is going on in our code:

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

In the first line, we are just importing the sys module. So nothing big going on there. Right? But what is happening in line number 2 & 3?

Let us break it down a little bit. First let us see what is present inside the parenthesis. So we have the code that looks like this:

sys.argv[1]

Now what is going on here? Well, we are making use of the sys module’s argv list. This is the list which will hold our input parameter values 1 & 2. But then why are we indexing it at 1? Well because index 0 will be pointing to the program name sum.py!

So we take the input values 1 & 2 from argv list and type cast it to integers. But why? Because every element of argv list will be a string. So we need to type cast it as required ourselves. Got it?

So from there on, the rest of the code is pretty straight forward. We will just add the two numbers and print the result!

So there you go! That is how we can read command line arguments in our Python code. Now if you have any questions about it, do let me know in the comments below. I will be more than happy to help!

So until next time, take care! 🙂

Want to learn how to check the file size in Python? Check this out!

Categories
DATA SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Check File Size In Python In Bytes

In this article, we will learn how to check the file size using Python code. So to begin with, we will first look at some of the functions that we can use to do so. But we will not just stop there. Instead, we will look at some example code as well.

So does that sound good for you? Yeah? Great! So then let us start right away!

Check File Size In Python Using OS Module

If you look at Python’s OS module, you will see that it’s path sub module has a getsize( ) function. So we can use this function to to check the size of a file!

So how does the code using this function look like? Take a look at it for yourself!

import os
file_path = 'hello.txt'
print(os.path.getsize(file_path))

So as you can see in the above code, we are calling the getsize( ) function with the file_path parameter. Now this is a parameter that has the file name along with it’s path.

Check File Size In Python
Check File Size In Python

In our case, we are accessing a file called hello.txt which is in the same directory as from where this Python code is run. So we did not have to give any folder name. But if your file lies in some other folder, then we need to give it’s full path.

Does that make sense? Great! So then how does the output look like? Well take a look at it yourself:

766

So is it just a number? Well yeah! But that is the number of bytes present in that file. Got it?! So, what it means is that the size of our file hello.txt is 766 bytes!

So it is quite easy to use the getsize( ) function then. Right?

But what happens if the file does not exist? Well, it just raises the os.error in that case!

Sounds good? Great! But this is not the only way for us to check the file size in Python. Alright? Here is another way to do it!

Check File Size Using OS Stat Module

So here is yet another way for us to check the file size – by making use of the OS stat module.

In this case, we will make use of the st_size member value of the os.stat module. This value is nothing but the size of a file. Alright? So then how does it’s code look like? Well, take a look at it here:

import os
filepath = 'hello.txt'
print(os.stat(filepath).st_size)

The output that we get here is once again:

766

Same as the previous example code’s value. Right?

Conclusion

So there you have it. Those are the simplest ways by which we can check the file size in Python. Working with files in Python is easy when we make use of built in modules. The OS module is one such module that will make our life easier.

So with this, I will end this article now. We will continue to look at some more Python modules in the future. But for now, I guess this should do it. Right? But if you have any queries on this, do let me know and I will be happy to help.

So until next time. Take care!

Want to know what are Python Reserved Keywords? Check this out!

Categories
COMPUTER SCIENCE DATA MINING DATA SCIENCE DATA VISUALIZATION PROGRAMMING PYTHON TUTORIALS

How To Get Current Timestamp In Python

Are you working on a Python program and now want to get the current timestamp in to your Python code? Then do not worry. Because we will now learn how to do just that!

Sounds good? Great! So let us start then!

Getting Started

Python comes with a variety of libraries to work with time. But among them, two of the libraries stand out for getting the current timestamp. These are the datetime library and the time library.

Get Current Timestamp In Python
Get Current Timestamp In Python

So we can use either of these Python libraries to get the current timestamp. Let us now learn how to use each of them. Alright?

Get Current Timestamp In Python Using Datetime Library

We can use Python’s datetime library to get the current timestamp. To do that, we will need to use it’s now() function. So the example code using datetime library to get the current timestamp looks something like this:

from datetime import datetime
ts = datetime.now().strftime("%B %d, %Y")
print("Current timestamp is: " + ts)

Here, the function datetime.now( ) will return the current timestamp. But the problem is, it is not in human readable format but rather in datetime object format. So we need to convert it from the datetime object to a human readable format. So to do that, we use another datetime function called strftime.

strftime – This function will convert the timestamp from datetime object to a human readable string.

So if you run the code above, you get an output that looks something like this:

'August 17, 2020'

As you can see, we now have our timestamp that is easy to read!

While this is one way for us to get our timestamp, we can use another library to do the same. And that is to use the time library. But how do we use it? Let us take a look at it in the next section.

Get Current Timestamp In Python Using Time Library

Just like the datetime library we saw earlier, we can use the time library to get our current timestamp. But how? To get current timestamp from the time library, we just need to call it’s time function.

Aah…not sure how to do that? Well, take a look at the code below:

import time
secondsFromEpoch = time.time()
print(secondsFromEpoch)

So what output do you get when you run the above code? Well, take a look at it for yourself:

1597651340.6727788

Wait a second! What the hell is going on over here? What are these numbers? They do not look like a timestamp! Right?

Well, you are right in a way. It indeed does not look like a timestamp we normally recognize. But what if I told you that it’s value is actually correct?

Wait what? How is it right? Well, the reason is because it is showing the total number of seconds that has passed since the epoch time.

Wait a minute! Now what is epoch time?

What Is Epoch Time?

Epoch time is the number of seconds that have passed since the midnight of January 1, 1970 GMT.

So when we print the value of secondsFromEpoch, the value that we see is the number of seconds passed since the Epoch Time!

Okay, all that is well and good. But how do we make it more readable?

Well to make it more readable, we will use another helper function from the time module called the localtime( ) function.

So using this function, we can change the above code to print a more readable timestamp value. It looks like this:

import time
secondsFromEpoch = time.time()
timeObj = time.localtime(secondsFromEpoch)
print("The current time is: %d/%d/%d %d:%d:%d" % (
timeObj.tm_mday, timeObj.tm_mon, timeObj.tm_year, timeObj.tm_hour, timeObj.tm_min, timeObj.tm_sec))

And this will give us the output:

The current time is: 17/8/2020 13:32:20

Now, this is more readable. Right?

So there you have it! This is how we can get the current timestamp in Python using the time module.

But are these the only two ways to get a timestamp? Well, no. As I said there are many libraries in Python we can use to get the current timestamp. But these are the two libraries that are easier to use.

Another library that comes to my mind is the calendar module. But this library works along with the time module. So what it does is, it uses the time tuple value given by time module’s gmtime( ) function.

It then converts this value to current time. Now the code looks something like this:

import calendar
import time

ts = calendar.timegm(time.gmtime())
print(ts)

So when you run this code, the output looks like this:

1597656083

So there you have it. These are some of the ways we can get our current timestamp. But if you have any doubts around it, do let me know in the comments below. Alright? So until next time, take care!

Want to learn how to extract data from a website using Python? Check this article!

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

Matplotlib Annotate Text In A Plot Using Python

In this article, we will learn how to annotate text in Matplotlib plot using Python. But if we only add annotation text to a plot, it will not look that great. Because we want that annotation to point to a particular location in the plot. Right? So we will also be looking at how we can add arrows to go with these annotations. By using arrows, we will be able to point it to a location on the plot as well.

Now does that sound good for you? Great! So then let u begin!

First thing first. Let us learn what an annotation is. Alright? Because knowing what an annotation does to a plot will help us know how to use it. Right? So let us start fro there.

What Is Annotation?

Annotation is a piece of text that we can add to a plot to add give some explanation. So that is all there is to it.

But then you may ask why we want to text annotate a Matplotlib? After all, we already have a way to add text inside a plot in Matplotlib. Right? We have already seen how we can do that in our earlier article. So then why do we need annotation as well?

Well, there is a simple reason for that. So you see, using text( ) function provided by Matplotlib is fine for simple texts. But for larger texts, we want to go for annotations because we can use arrows here! So that way we can use larger texts but still point this text a point on the plot using arrows!

So that is the real benefit of using annotated text in Matplotlib plots rather than normal text( ) function. I hope this is clear to you by now!

So with that, let us see how we can text annotate a point in the Matplotlib plot.

How To Use Matplotlib To Annotate Text In A Plot

So how do we add annotation to a Matplotlib plot? Well using the annotate( ) function from the pyplot module, of course!

So the pyplot module provides us with an annotate( ) function as well! So using this, we can add annotations to our plot. But how does the signature of this function look like? Let us take a look at it:

annotate(text, xy, xytext, arrowprops)

So this is the basic signature of the annotate( ) function. But this signature is not all of it. It is missing few more arguments. But they are all optional. So we have skipped what is not required.

So what do these arguments mean? To understand, let us take a look at them one by one:

text - This is the annotation text
xy - This is the x & y co-ordinates of the point on our plot to which we will be adding the annotation
xytext - This is the x & y co-ordinates where we want our annotated text to appear
arrowprops - This will define the properties of our arrow

I know this is all looking overwhelming. But it will become clear to you once we looked at an example code using that. Alright? So let us look at that right now!

Matplotlib Annotate Text Example

So here is our example code:

import matplotlib.pyplot as plt
y = [8, 10, 11, 12, 10, 9, 10, 8, 7, 11, 10, 9]
plt.plot(y)
plt.annotate('this is the point I want to point!',
xy=(4, 10), xytext=(5, 10.75), arrowprops=dict(facecolor='red'))
plt.show()

As you can see, we are using a simple data set y with 12 values in it. We are then calling the normal plot( ) function to plot this data. But the real meat is in our 4th line where we are using our annotate( ) function.

So as you can see, we are passing a number of parameters here. The first is the text parameter which simply reads “this is the point I want to point!”.

The next is the xy parameter which is the point we want to annotate. This is followed by the xytext parameter which allows us to set the location where we wan our text to appear.

And finally we have the arrowprops parameter which allows us to set the arrow properties.

So using this code we get the following plot:

Matplotlib Plot With Annotate Text
Matplotlib Plot With Annotate Text

So there you have it. This is how you can use Python Matplotlib library to create a plot with annotated text. I hope it was easy enough for you. But if you have any questions, do let me know in the comments below and I will be happy to help.

So with that, I will end this tutorial now. Hope you have a nice day! 🙂

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION PROGRAMMING PYTHON TUTORIALS

Matplotlib Add Text Inside A Plot Using Python

In this article, we will learn how to add text inside a plot using Matplotlib. But not just that. We will also look at how we can position this text inside the plot.

In our earlier articles, we had already seen how we can add titles and axis labels to our plot. Right? So this article will be a follow up of that. But only that we will be adding text inside the plot itself. This is unlike others where we added labels and titles outside the plot.

So how do we go about doing this? How can we add text inside a plot? If we can, can it be positioned wherever we want within the plot? These are some of the questions we will be answering next!

So let us start with the first question:

How To Add Text Inside A Matplotlib Plot Using Python

So to answer this, let us draw a simple plot using the following code:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 20, 1)
y = np.arange(0, 2, 0.1)
plt.plot(x, y)
plt.show()

So this will give us a simple plot with a linear line that looks like this:

A simple line plot
A simple line plot

Okay. This is great! But now how do we go about adding a piece of text inside this plot? Let me say I want a piece of text that reads “This is cool!” written above the blue line, but somewhere in the middle? Can I do that in Matplotlib?

The text( ) function

Of course you can! Matplotlib provides you with a very specific function for you to do just that! The function is called the text( ) function and it is part of the plt module of Matplotlib.

So what does the signature of this text( ) function look like? Here it is:

plt.text(x, y, text)

In the above text( ) function signature,w e see that it takes in 3 parameters – x, y, & text. But what do they do? Well, the parameters x & y gives the co-ordinates where the text is to be written. While the “text” parameter specifies the text we want to write!

So how does this all workout for us where we want to say “This is cool!” above the blue line? Well, first we need to set the x & y co-ordinates.

From the plot above, we can choose a value that is above the line and somewhere in the middle. Right? So what would be that value? Well how about it being 5 along the x-axis and 1 along the y-axis? In which case, we can call the text( ) function as follows:

plt.text(5, 1, 'This is cool!')

We will call this function just before plotting. So our final code will then become:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 20, 1)
y = np.arange(0, 2, 0.1)
plt.text(5, 1, 'This is cool!')
plt.plot(x, y)
plt.show()

So how will the plot look like for this code? Take a look at it for yourself!

Matplotlib Add Text Inside A Plot
Matplotlib Add Text Inside A Plot

Now that is what we wanted right?!

So there you have it. This is how we can add text inside a plot in Matplotlib using Python. Hope it was pretty easy to follow. But if you have any questions, do let me know in the comments below. I will be more than happy to help!

So until next time, have a nice day! 🙂

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION PROGRAMMING PYTHON TUTORIALS

How To Plot Scatter Plot In Python Using Matplotlib

In this article, we will learn how to plot a Scatter plot in Python using Matplotlib. But before we do that, we will learn what a Scatter plot is and what all options are there for us to use. Sounds good? Great! Then let us start!

What Is A Scatter Plot?

A scatter plot is a type of plot that we can use to display values from two sets of data. So what happens is, we will take two sets of data of same length & pair them together. We then use this pair to plot the scatter plot. It is very important for you to remember here that both the data sets have to be of the same length!

But there is another thing to note here. It is that the scatter plot will have only points drawn and no lines in them. So in other words the points in the plot will not be connected together. But just the points scattered across the chart. Hence the name scatter plot!

What Is The Use Of A Scatter Plot?

We can use Scatter plot to see any correlation between two data sets. So what happens is, similar points get grouped together in the scatter plot. Now this can be a very valuable insight for us. Especially when looking at non linear relationships between the two datasets! Does that make sense?

So we can use a scatter plot to find any relationship between data points.

So now that we know the uses of a scatter plot, let us see how to plot it. But to do so, we need two sets of data. Right? So what do we do for that? Where can we find it?

How To Plot A Scatter Plot In Python

Here is what we will do. We will use our good old random function randn( ) from the Numpy library for that. Alright? So check this code:

import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100)
y = np.random.randn(100)
plt.scatter(x, y);
plt.show()

So this is the code that will generate us a scatter plot using two sets of random data. But what is going on here? Let us go through the code line by line:

So in line 1 and 2, we are importing our Matplotlib and Numpy libraries.

But what is going on in line 3 & 4? Well, as I said earlier, we need two sets of data for our scatter plot. So we are using Numpy’s randn( ) to generate these data sets. We will assign them to variables x & y.

Next in line 5, we call our good old plt module’s scatter( ) function and passing x & y to it. So this is the function that will generate our scatter plot using x & y data!

Finally we call the usual plt.show( ) function to display our resulting scatter plot. So here is how it looks like:

A basic Scatter Plot With 100 variables
A basic Scatter Plot With 100 variables

So as you can see, there are 100 points taken from x & y variables to be plotted along the X & Y axis. So if there are any points that are similar, they will converge together here!

How To Change The Size Of A Marker

The above chart is all well and good, but is there a way to control the size, color & marker type in it? Well, lucky for us Matplotlib does give us an option for this. So to do so, we need to set the s, c & marker parameters in our plt.scatter( ) function!

So here is a simple example code that changes the size and color of our plot markers:

import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(100)
y = np.random.randn(100)
size = 20*np.random.randn(100)
colors = np.random.rand(100)
marker = "^"
plt.scatter(x, y, s=size, c=colors, marker=marker);
plt.show()

And here is how our final scatter plot would look like:

Scatter Plot with changed color, size & marker type
Scatter Plot with changed color, size & marker type

So there you have it! This is how we plot a Scatter Plot in Python. I hope this was easy enough for you to follow. But if you have any doubts, do let me know in the comments below. I will be more than happy to help!

Alright? So then see you until next time. take care! 🙂

What to learn how to plot a Pie chart using Matplotlib? Take a look at this!