Answer to Question #319921 in Python for sung jin woo

Question #319921

1.How do you specify values for the X-axis of a line chart in python?

2.How do you specify labels for the axes of a chart in python?

3.How do you plot multiple line charts on the same axes in python?

1
Expert's answer
2022-03-30T17:46:27-0400

To specify values for the X-axis of a line chart in python:

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = [42, 2, 32, 75, 100]
y = [7, 12, 25, 79, 3]
default_x_ticks = range(len(x))
plt.plot(default_x_ticks, y)
plt.xticks(default_x_ticks, x)
plt.show()

To specify labels for the axes of a chart in python:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [0, 1, 2, 3, 4]
y = [2, 4, 6, 8, 12]

# Plotting

plt.plot(x, y)

# Add x-axis label

plt.xlabel('X-axis Label')

# Visualize

plt.show()

To plot multiple line charts on the same axes in python:

import matplotlib.pyplot as plt
# line 1 points
x1 = [10,20,30]
y1 = [40,20,10]
# plotting the line 1 points 
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [10,40,30]
y2 = [30,20,50]
# plotting the line 2 points 
plt.plot(x2, y2, label = "line 2")
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Set a title of the current axes.
plt.title('Two or more lines on same plot with suitable legends ')
# show a legend on the plot
plt.legend()
# Display a figure.
plt.show()


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS