3.creating Line Charts and Scatter Charts
3.creating Line Charts and Scatter Charts
3.creating Line Charts and Scatter Charts
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')
# 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]
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()