If you’re interested in combining your affection for art with your programming skills, then Python programming may be just the right thing for you.
Getting this useful combination to work properly may take some effort, but once you’ve gotten the hang of it, you’ll be able to create stunning graphics with Python.
Python provides various Libraries for data visualization like Matplotlib, Seaborn, etc., but here we are using the most popular and widely used library, Matplotlib.
Step 1: Installation of Matplotlib
Before we get ahead with the steps to draw a picture in Python, we first need to install the required library Matplotlib. You can install it using pip by running the below command in your terminal:
1 |
pip install matplotlib |
Step 2: Importing Necessary Libraries
After successful installation of Matplotlib, import necessary libraries in Python:
1 2 |
import matplotlib.pyplot as plt import numpy as np |
Step 3: Create Data for Picture
Next, we need to create some data to be displayed as a picture. Let’s create a 2D numpy array with random numbers:
1 |
data = np.random.random((50,50)) |
Here, data will be a 2D array with 50 rows and 50 columns – each filled with a random number between 0 and 1.
Step 4: Drawing Picture
Now that we have the data, let’s use matplotlib to create an image from this data with the imshow
function:
1 2 |
plt.imshow(data, cmap='hot', interpolation='nearest') plt.show() |
This script will render an image from the 2D array, using a colormap of ‘hot’ and ‘nearest’ for the interpolation method, and plt.show()
will present our picture.
Here is the Full Code:
1 2 3 4 5 6 7 |
import matplotlib.pyplot as plt import numpy as np data = np.random.random((50,50)) plt.imshow(data, cmap='hot', interpolation='nearest') plt.show() |
Conclusion
As you can observe, drawing pictures with Python code isn’t as complicated as it seems. With the help of Python libraries like Matplotlib, almost anyone can draw appealing images.
Now, you can fully explore and get more creative by adding customization to your images like labels, titles, etc. Expanding proficiency with Matplotlib will open up a whole new world of options for visualizing data.