Python uses matplotlib to plot the sine and Cosine curves, pythonmatplotlib

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.