Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Set Ticks and Limits | Scatter Plots
Visualization in Python with matplotlib

book
Set Ticks and Limits

Tâche

Swipe to start coding

Using the .set() function, set the following parameters:

  • limit values on the x-axis to (0, 30000);
  • limit values on the y-axis to (40, 80);
  • set ticks on the x-axis so that they will have a step of 5000 (for instance, 0, 5000, 10000, 15000, ...). You can use the np.arange(start, end, step) function there. It will generate numbers from start to end with the step of step.

Solution

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0)

# Create Figure and Axes objects
fig, ax = plt.subplots()

# Initialize the scatter plot
ax.scatter(data['gdp per capita'], data['life exp'])

# Set the plot limits and ticks
ax.set(xlim = (0, 30000), ylim = (40, 80), xticks = np.arange(0, 30000, 5000))

# Display the plot
plt.show()

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 4
single

single

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Load the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/ed80401e-2684-4bc4-a077-99d13a386ac7/gapminder2017.csv', index_col = 0)

# Create Figure and Axes objects
fig, ax = plt.subplots()

# Initialize the scatter plot
ax.scatter(data['gdp per capita'], data['life exp'])

# Set the plot limits and ticks
ax.set(___, ___, xticks = np.arange(___, ___, ___))

# Display the plot
plt.show()

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt