0% found this document useful (0 votes)
14 views

matplotlib

Uploaded by

Ankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

matplotlib

Uploaded by

Ankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

matplotlib

April 24, 2024

[10]: import pandas as pd


import numpy as np
import matplotlib.pyplot as plt

[6]: x = np.random.rand(50)
y = np.random.rand(50)

[7]: x

[7]: array([0.30209178, 0.36465146, 0.6517227 , 0.64217784, 0.18394955,


0.43992628, 0.97284552, 0.44464244, 0.73957774, 0.85068249,
0.59514177, 0.59043099, 0.96952162, 0.63557871, 0.10673337,
0.12619059, 0.55984256, 0.54137774, 0.975966 , 0.83401489,
0.30622566, 0.65870933, 0.7956874 , 0.53831361, 0.28125521,
0.08301803, 0.06846407, 0.77602031, 0.09198045, 0.87324448,
0.45952519, 0.83970149, 0.98286191, 0.33175433, 0.94585855,
0.90141068, 0.99288648, 0.03855211, 0.38754614, 0.17746932,
0.30905146, 0.75663492, 0.72290799, 0.46104689, 0.1298827 ,
0.43302667, 0.53864503, 0.78307543, 0.03724502, 0.49931759])

[8]: y

[8]: array([0.36763653, 0.64688348, 0.89869257, 0.05024984, 0.79321601,


0.03710734, 0.8674068 , 0.77215801, 0.99895439, 0.528465 ,
0.72186661, 0.39146745, 0.01891229, 0.59990627, 0.40357562,
0.25447947, 0.8132269 , 0.10533152, 0.19602344, 0.56766765,
0.96943092, 0.68680141, 0.12110709, 0.43979179, 0.46963497,
0.46638116, 0.92031331, 0.71507098, 0.14622799, 0.78520582,
0.93304859, 0.55832187, 0.04519302, 0.5238949 , 0.9955732 ,
0.4405559 , 0.88289017, 0.44201191, 0.63381631, 0.96928366,
0.15281517, 0.69783284, 0.85963521, 0.51254292, 0.89863094,
0.06013336, 0.21766704, 0.10266528, 0.5514191 , 0.45001613])

[9]: plt.scatter(x,y)# to understand the two variable

[9]: <matplotlib.collections.PathCollection at 0x7f1ac50dcbb0>

1
[12]: df = pd.DataFrame(np.random.randn(1000), columns = ['data'], index = pd.
↪date_range('2023-03-29', periods =1000))

[13]: df.plot(figsize = (20,8))

[13]: <AxesSubplot: >

2
[15]: x = [1,2,3,4,5]
y = [9,5,6,7,8]
plt.plot(x,y)

[15]: [<matplotlib.lines.Line2D at 0x7f0c58578bb0>]

[18]: plt.plot(x,y)
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")

3
[19]: plt.plot(x,y)
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

4
[23]: plt.plot(x,y, color = "red")
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

5
[25]: plt.plot(x,y, color = "red", marker = "o")
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

6
[26]: plt.plot(x,y, color = "red", marker = "o", linestyle = '--')
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

7
[27]: plt.plot(x,y, color = "red", marker = "o", linestyle = '--', linewidth = 3)
plt.title("line plot to understand the syntax")
plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

8
[28]: plt.plot(x,y, color = "red", marker = "o", linestyle = '--', linewidth = 3,␣
↪markersize = 7)

plt.title("line plot to understand the syntax")


plt.xlabel("x points")
plt.ylabel("y points")
plt.show()

9
[31]: plt.plot(x,y, color = "red", marker = "o", linestyle = '--', linewidth = 3,␣
↪markersize = 7)

plt.title("line plot to understand the syntax")


plt.xlabel("x points")
plt.ylabel("y points")
plt.grid()
plt.show()

10
[37]: x = [1,2,3,4,5]
y1= [9,5,6,7,8]
y2= [9,8,12,7,8]

plt.plot(x,y1, color = "red", marker = "o", linestyle = '--', linewidth = 3,␣


↪markersize = 7,label = 'line y1')

plt.plot(x,y2, color = "b", marker = "o", linestyle = '--', linewidth = 3,␣


↪markersize = 10, label = 'line y2')

plt.xlabel("x data axis")


plt.xlabel("y data axis")

plt.title("line to understand")

plt.show()

11
[38]: x = [1,2,3,4,5]
y1= [9,5,6,7,8]
y2= [9,8,12,7,8]

plt.plot(x,y1, color = "red", marker = "o", linestyle = '--', linewidth = 3,␣


↪markersize = 7,label = 'line y1')

plt.plot(x,y2, color = "b", marker = "o", linestyle = '--', linewidth = 3,␣


↪markersize = 10, label = 'line y2')

plt.xlabel("x data axis")


plt.xlabel("y data axis")

plt.title("line to understand")
plt.legend()

plt.show()

12
[39]: x = np.linspace(0,10,100)

[40]: x

[40]: array([ 0. , 0.1010101 , 0.2020202 , 0.3030303 , 0.4040404 ,


0.50505051, 0.60606061, 0.70707071, 0.80808081, 0.90909091,
1.01010101, 1.11111111, 1.21212121, 1.31313131, 1.41414141,
1.51515152, 1.61616162, 1.71717172, 1.81818182, 1.91919192,
2.02020202, 2.12121212, 2.22222222, 2.32323232, 2.42424242,
2.52525253, 2.62626263, 2.72727273, 2.82828283, 2.92929293,
3.03030303, 3.13131313, 3.23232323, 3.33333333, 3.43434343,
3.53535354, 3.63636364, 3.73737374, 3.83838384, 3.93939394,
4.04040404, 4.14141414, 4.24242424, 4.34343434, 4.44444444,
4.54545455, 4.64646465, 4.74747475, 4.84848485, 4.94949495,
5.05050505, 5.15151515, 5.25252525, 5.35353535, 5.45454545,
5.55555556, 5.65656566, 5.75757576, 5.85858586, 5.95959596,
6.06060606, 6.16161616, 6.26262626, 6.36363636, 6.46464646,
6.56565657, 6.66666667, 6.76767677, 6.86868687, 6.96969697,
7.07070707, 7.17171717, 7.27272727, 7.37373737, 7.47474747,
7.57575758, 7.67676768, 7.77777778, 7.87878788, 7.97979798,

13
8.08080808, 8.18181818, 8.28282828, 8.38383838, 8.48484848,
8.58585859, 8.68686869, 8.78787879, 8.88888889, 8.98989899,
9.09090909, 9.19191919, 9.29292929, 9.39393939, 9.49494949,
9.5959596 , 9.6969697 , 9.7979798 , 9.8989899 , 10. ])

[41]: plt.plot(x, np.sin(x))


plt.plot(x, np.cos(x))

[41]: [<matplotlib.lines.Line2D at 0x7f0c549b3b20>]

[42]: plt.plot(x, np.sin(x))

[42]: [<matplotlib.lines.Line2D at 0x7f0c54999780>]

14
[43]: plt.plot(x, np.sin(x),'--')

[43]: [<matplotlib.lines.Line2D at 0x7f0c54a22170>]

15
[46]: plt.figure()
plt.subplot(1,2,1)
plt.plot(x, np. sin(x))

plt.figure()
plt.subplot(4,5,1)
plt.plot(x, np. cos(x))

plt.show()

16
[47]: plt.figure()
plt.subplot(2,2,1)
plt.plot(x, np. sin(x))

plt.figure()
plt.subplot(4,4,4)
plt.plot(x, np. cos(x))

plt.figure()
plt.subplot(1,2,1)

17
plt.plot(x, np. tan(x))

plt.figure()
plt.subplot(2,5,1)
plt.plot(x, np. cos(x+1))

plt.show()

plt.show()

18
[48]: plt.plot(x, np. sin(x-0),color = "blue")
plt.plot(x, np. sin(x-1),color = "g")
plt.plot(x, np. sin(x-2),color = (1.0,0.5,0))

[48]: [<matplotlib.lines.Line2D at 0x7f0c54629030>]

19
[49]: plt.plot(x, np. sin(x-0),color = "blue")
plt.ylim(1,2,3,4)
plt.xlim(0.5,0.10,0.15,0.20)
plt.show()

/tmp/ipykernel_1666/1958219425.py:2: MatplotlibDeprecationWarning: Passing the


emit parameter of set_ylim() positionally is deprecated since Matplotlib 3.6;
the parameter will become keyword-only two minor releases later.
plt.ylim(1,2,3,4)
/tmp/ipykernel_1666/1958219425.py:3: MatplotlibDeprecationWarning: Passing the
emit parameter of set_xlim() positionally is deprecated since Matplotlib 3.6;
the parameter will become keyword-only two minor releases later.
plt.xlim(0.5,0.10,0.15,0.20)

20
[51]: x = [1,2,3,4,5]
y = [9,5,6,7,8]

plt.plot(x, np. sin(x-0),color = "blue")


plt.ylim(1,2,3,4,5,6)
plt.xlim(5,10,15,8)
plt.show()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[51], line 4
1 x = [1,2,3,4,5]
2 y = [9,5,6,7,8]
----> 4 plt.plot(x, np. sin(x-0),color = "blue")
5 plt.ylim(1,2,3,4,5,6)
6 plt.xlim(5,10,15,8)

TypeError: unsupported operand type(s) for -: 'list' and 'int'

[ ]:

21

You might also like