Intermediate Python for Data Science learning 2

Histograms

from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib?ex=7

  • Build a histogram (1)

life_exp, the list containing data on the life expectancy for different countries in 2007, is available in your Python shell.

To see how life expectancy in different countries is distributed, let's create a histogram of life_exp.

# import matplotlib.pyplot

import matplotlib.pyplot as plt

# Create histogram of life_exp data. Do not specify the number of bins; Python will set the number of bins to 10 by default for you.

plt.hist(life_exp)

# Display histogram
plt.show()

  • Build a histogram (2): bins

To control the number of bins to divide your data in, you can set the bins argument.

# Build histogram with 5 bins
plt.hist(life_exp, bins = 5)

# Show and clean up plot
plt.show()
plt.clf()

# Build histogram with 20 bins
plt.hist(life_exp,bins = 20)

# Show and clean up again
plt.show()
plt.clf()

  • Build a histogram (3): compare

Let's do a similar comparison. life_exp contains life expectancy data for different countries in 2007. You also have access to a second list now, life_exp1950, containing similar data for 1950. Can you make a histogram for both datasets?

# Histogram of life_exp, 15 bins
import matplotlib.pyplot as plt
plt.hist(life_exp, bins = 15)

# Show and clear plot
plt.show()
plt.clf()

# Histogram of life_exp1950, 15 bins
plt.hist(life_exp1950, bins = 15)

# Show and clear plot again
plt.show()
plt.clf()

  • Choose the right plot (1)

You're a professor teaching Data Science with Python, and you want to visually assess if the grades on your exam follow a particular distribution. Which plot do you use?

->Histogram

  • Choose the right plot (2)

You're a professor in Data Analytics with Python, and you want to visually assess if longer answers on exam questions lead to higher grades. Which plot do you use?

->Scatter plot

原文地址:https://www.cnblogs.com/keepSmile/p/7797964.html