matplotlib.pyplot Flashcards
Import the function which enables you to create simple charts
import matplotlib.pyplot as plt
Which function is the most commonly used from the matplotlib module?
pyplot, usually as “plt” variable name
Create a line plot from x and y datasets
plt.plot(x, y)
Create a scatterplot from x and y datasets
plt.scatter(x, y)
Show the created plot
plt.show()
clean axis
plt.cla()
clean figure
plt.clf()
erase plots and release memory
plt.close()
Create a histogram from dataset x
plt.hist(x)
Create a histogram from dataset x with 30 bins
plt.hist(x, bins=30)
Modify the scale of the x axis to logarithmic scale
plt.xscale(‘log’)
We need a standard world development chart based on GDP per capita and life expectancy. You also have population to include as a third dimension (size), and you have information about the continent (color).
Corresponding numpy arrays:
gdp, life, pop, col.
Make the points slightly transparent to that they can be distinghuised if they are overlapping.
plt.scatter(x = gdp, y = life, s = pop, c = col, alpha = 0.8)
Include the following options on the scatterplot named plt already existing:
X label: “GDP”
Y label: “Life expectancy”
Title: “World Development”
X axis ticks: “1000, 10000, 100000” with the labels “1K”, “10K”, “100K”
plt. xlabel(“GDP”)
plt. ylabel(“Life expectancy”)
plt. title(“World Development”)
plt. xticks([1000,10000,100000], [“1K”, “10K”, “100K”])
Add custom text.
At coordinates 1550, 71, add “India”.
plt.text(1550, 71, “India”)
Turn on the grid
plt.grid(True)