Python Matplotlib - How to Create Contour Plot in Python

in Education2 years ago

In this post, we look at How to Create Contour Plot in Python. Previously we looked at How to create the Box plot in Python using Matplotlib. It was good for specific set of the data. And it was worth it for plenty of configurations. You can do variety of ways improve that plot and present it in to change how the data is shown in this type of the plot. Today we explore the contour plot or other ways to say it is the contoour map.

Contour plot can be done in many ways. It would help you display the distribution, density, altitude, height and also variety of data. But it all depends on how you are representing it and how you choose to plot it through the data that is given to you. As you can see this would be pretty easier to draw and display if you can handle the data in much better way as well as you know it can get the visualization pretty effectively.

I can recommend setting up the environemnt on the desktop. You can also use the setups on cloud like the codeanywhere and the coder.com, which is pretty easy to use as well. I prefer most of the setups like those.

And I have created a video to give you an overview on How to Create Contour Plot in Python. You should give this below video a look.



First thing make sure to setup the libraries. And also check the python version.

python --version

Most of the libraries that I want to use in this type of the tutorial are here.

pip install numpy, jupyter matplotlib

Now let's reference some of these libraries. We are using the jupyter notebook here so we are making a notebook once we run the following command.

jupyter notebook

and then we add the reference to the library.

import numpy as np
import matplotlib.pyplot as plt

Now lets try and add the dummy or say sample data.

X, Y = np.meshgrid(np.linspace(-5, 10, 100),
np.linspace(-5, 10, 100))
Z = np.sqrt(X ** 2 + Y ** 2)

now let's try and add entire code for the plot.

import numpy as np
import matplotlib.pyplot as plt
X, Y = np.meshgrid(np.linspace(-5, 10, 100),
np.linspace(-5, 10, 100))
Z = np.sqrt(X ** 2 + Y ** 2)
fig, ax = plt.subplots()
ax.contour(Z)
plt.show()

You can now see the above code in the image for the rerenfece ifyou wish to save it.

And nowour job is to run this code. So for that I assume you have file called example.py and when we run the file through the python interpreter it would be showing like.

python example.py

That's it now check the output.

Once you get the first map you can easily go on with another and make changes like adding levels to the data and that would even make the contour more complex. You can also add the color to the contour map. You can check out the styles and width. You would also find the labels with the data of that type. There is also the contourf function that you should be exploring there. And you can get different type of the data output in the map.

I hope that you have enjoyed the content. I would appreciate if you can like, share and subscribe to my channel. You can check the above video for the channel and also other playstlists are there. So you would be able to easily check my content there as well.I have made plenty of videos in this context and data science has been covered alot. I just need more share, likes and subscription so that my content would reach more people. That is the plan for now.

If you happen to like this content, do give me feedback over there and that would help me improve my efforts in near future.

Sort:  

Congratulations @devpress! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Our Hive Power Delegations to the October PUM Winners
Feedback from the November Hive Power Up Day
Hive Power Up Month Challenge - October 2023 Winners List

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.