Python uses matplotlib to plot the sine and Cosine curves, pythonmatplotlib
This example describes how to use matplotlib to draw a sine and cosine curve in Python. We will share this with you for your reference. The details are as follows:
1. Introduction
Keywords: Drawing Library
Official Website: http://matplotlib.org
Code 2
Import numpy as npimport matplotlib. pyplot as plt # linex = np. linspace (-np. pi, np. pi, 256, endpoint = True) # define the cosine function sine function c, s = np. cos (x), np. sin (x) plt. figure (1) # drawing, x as the abscissa, and c as the ordinate plt. plot (x, c, color = "blue", linestyle = "-", label = "COS", alpha = 0.5) plt. plot (x, s, "r *", label = "SIN") # Add the title plt. title ("COS & SIN") ax = plt. gca () ax. spines ["right"]. set_color ("none") ax. spines ["top"]. set_color ("none") ax. spines ["left"]. set_position ("data", 0) ax. spines ["bottom"]. set_position ("data", 0) ax. xaxis. set_ticks_position ("bottom") ax. yaxis. set_ticks_position ("left") plt. xticks ([-np. pi,-np. pi/2, 0, np. pi/2, np. pi], [R' $-\ pi $ ', R' $-\ pi/2 $', R' $0 $ ', r' $ + \ pi/2 $ ', R' $ + \ pi $']) plt. yticks (np. linspace (-1, 1, 5, endpoint = True) for label in ax. get_xticklabels () + ax. get_yticklabels (): label. set_fontsize (16) label. set_bbox (dict (facecolor = "white", edgecolor = "None", alpha = 0.2) # The Legend displays plt. legend (loc = "upper left") # display the grid plt. grid () # display range # plt. axis ([-0.5,-, 1]) plt. fill_between (x, np. abs (x) <0.5, c, c> 0.5, color = "green", alpha = 0.25) t = 1plt. plot ([t, t], [0, np. cos (t)], "y", linewidth = 3, linestyle = "--") plt. annotate ("cos (1)", xy = (t, np. cos (1), xycoords = "data", xytext = (+ 10, + 30), textcoords = "offset points", arrowprops = dict (arrowstyle = "-> ", connectionstyle = "arc3, rad =. 2 ") # displays the drawing plt. show ()
Three running results