3.creating Line Charts and Scatter Charts

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Creating Line Charts and Scatter Charts

from matplotlib import pyplot as plt


from matplotlib import style

style.use('ggplot')
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.plot(x,y,,linewidth=5)
plt.plot(x2,y2, linewidth=5)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
Here, as you can see, the only reference to styling that we've made is the
style.use() function, as well as the line width changes. We could also
change the line colors if we wanted, instead of using the default colors,
and get a chart like:
from matplotlib import pyplot as plt
from matplotlib import style

style.use('ggplot')
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.plot(x,y,'g',label='line one', linewidth=5)
plt.plot(x2,y2,‘r ',label='line two',linewidth=5)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.legend()
plt.grid(True,color='k') k=black
plt.show()
Up to this, everything is about the same, but now you can see we've added
another parameter to our plt.plot(), which is "label.“
We need to call plt.legend() to show the legend for all lines. It's important
to call legend AFTER you've plotted what you want to be included in the
legend.
Customization of Plots
Changing Line color, width and style ,markersize
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3,4,5,6]
# corresponding y axis values
y = [2,4,1,5,2,6]
# plotting the points
plt.plot(x, y, color='green', linestyle='dashed', linewidth =
3, marker='o', markerfacecolor='blue', markersize=12)
# setting x and y axis range
plt.ylim(1,8)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')

# giving a title to my graph


plt.title('Some cool customizations!')

# function to show the plot


plt.show()
Creating Scatter Chart
First Way of Creating Scatter Chart
Second Way of Creating Scatter Chart
Syntax of scatter () function is –
matplotlib.pyplot.scatter(a, b, marker=<type>)
import matplotlib.pyplot as plt

# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]

# plotting points as a scatter plot


plt.scatter(x, y, label= "stars", color= "green",
marker= "*", s=30)

# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()

# function to show the plot


Here, we use plt.scatter() function to plot a scatter plot.
Like a line, we define x and corresponding y – axis values here as well.
marker argument is used to set the character to use as marker. Its size can
be defined using s parameter.

You might also like