Nagendra18/Matplotlib-Seaborn
0
1import streamlit as st2import matplotlib.pyplot as plt3import pandas as pd4import numpy as np5 6 7st.title("Line Plot using Object-Oriented Approach")8st.markdown("<hr style='border: 2px dashed blue;'>", unsafe_allow_html=True)9 10st.write("""11In this example, we create a line plot using Matplotlib's object-oriented approach. We first create a figure object and then add axes to it, which allows us to customize the plot's layout and style.12""")13st.markdown("""14```python 15fig = plt.figure(figsize=(3, 4), facecolor="g")16axes = fig.add_axes([0, 0, 1, 1]) # [left, bottom, width, height]17 18 19axes.plot([3, 4, 5, 7], [7, 3, 4, 5])20 21 22st.pyplot(fig) ``` 23""")24fig = plt.figure(figsize=(3, 4), facecolor="g")25axes = fig.add_axes([0, 0, 1, 1]) 26axes.plot([3, 4, 5, 7], [7, 3, 4, 5])27st.pyplot(fig)28 29 30st.title("Simple Line Plot using plt.plot()")31 32st.write("""33In this example, we use Matplotlib's **plt.plot()** function to create a simple line plot. The function takes two lists or arrays: the first for the x-axis values and the second for the y-axis values.34""")35 36st.markdown("""37```python 38plt.plot([3, 4, 5, 7], [7, 3, 4, 5])39plt.title("Simple Line Plot")40plt.xlabel("X-axis")41plt.ylabel("Y-axis")42st.pyplot(plt) ``` """)43plt.plot([3, 4, 5, 7], [7, 3, 4, 5])44 45st.pyplot(plt)46 47 48st.title("Line Plot with Markers using Object-Oriented Approach")49st.write("",divider=True)50 51st.write("""52In this example, we create a line plot using the object-oriented approach in Matplotlib and add **markers** to highlight the data points. We use the `marker="o"` argument to display circular markers at each point on the line.53""")54st.markdown("""55```python56 57fig = plt.figure(figsize=(3, 4), facecolor="b")58axes = fig.add_axes([0, 0, 1, 1]) # [left, bottom, width, height]59 60axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="o")61st.pyplot(fig) ``` """)62fig = plt.figure(figsize=(3, 4), facecolor="b")63axes = fig.add_axes([0, 0, 1, 1]) 64 65axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="o")66st.pyplot(fig)67 68st.title("Common Marker Styles in Matplotlib")69st.write("",divider=True)70 71markers = {72 "Circle": "o",73 "Square": "s",74 "Triangle": "^",75 "Diamond": "D",76 "Point": ".",77 "Pixel": ",",78 "Cross": "x",79 "Pentagon": "p"80}81 82 83st.header("Common Line Styles:")84st.write("",divider=True)85markers = {86"-": "Solid line (default)",87"--": "Dashed line",88"-.": "Dash-dot line",89":": "Dotted line",90}91 92 93st.title("Line Plot with Markers using Object-Oriented Approach")94st.write("", divider=True)95 96st.write("""97In this example, we create a line plot using the object-oriented approach in Matplotlib and add **markers** to highlight the data points. We use the `marker="s"` argument to display square markers at each point on the line, and a dashed line style for the line.98""")99 100st.markdown("""101```python102import matplotlib.pyplot as plt103 104fig = plt.figure(figsize=(3, 4), facecolor="g")105 106axes = fig.add_axes([0, 0, 1, 1])107 108axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="s", linestyle="--")109 110st.pyplot(fig) ``` """)111 112fig = plt.figure(figsize=(3, 4), facecolor="r")113 114axes = fig.add_axes([0, 0, 1, 1])115 116axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="s", linestyle="--")117 118st.pyplot(fig) 119 120 121st.write(" `Markersize`: The size of the markers is set to of(8) points, making them larger and more prominent.")122st.write("", divider=True)123st.markdown("""124```python125fig = plt.figure(figsize=(3, 4), facecolor="g")126axes = fig.add_axes([0, 0, 1, 1]) # last, bottom, height, width127axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="s", linestyle=" ", markersize=8)128 129 130st.pyplot(fig)131``` """)132fig = plt.figure(figsize=(3, 4), facecolor="g")133axes = fig.add_axes([0, 0, 1, 1]) 134axes.plot([3, 4, 5, 7], [7, 3, 4, 5], marker="s", linestyle=" ", markersize=8)135st.pyplot(fig)136 137st.write(" `linewidth=<value>`: You can set linewidth to any numeric value that represents the thickness of the line in points. The default line width is usually 1.0, but you can adjust it as needed.")138st.write("", divider=True)139st.markdown("""140```python141fig=plt.figure(figsize=(3,4),facecolor="g")142axes=fig.add_axes([0,0,1,1]) # last,bottom,height,width143axes.plot([3,4,5,7],[7,3,4,5],marker="h",linestyle="--",markersize=8,linewidth=5144st.pyplot(fig) 145``` """)146fig=plt.figure(figsize=(3,4),facecolor="g")147axes=fig.add_axes([0,0,1,1]) # last,bottom,height,width148axes.plot([3,4,5,7],[7,3,4,5],marker="h",linestyle="--",markersize=8,linewidth=5)149st.pyplot(fig)150 151 152st.write(""" `Title`:The title should clearly describe what the plot is showing. For example, "Speed vs Distance" indicates that the plot illustrates the relationship between speed and distance""")153st.write("", divider=True)154st.markdown("""155```python156 157plt.plot([3,4,5,7],[7,3,4,5])158plt.xlabel("speed")159plt.ylabel("distance")160plt.title("speed vs distance") ``` """)161plt.plot([3,4,5,7],[7,3,4,5])162plt.xlabel("speed")163plt.ylabel("distance")164plt.title("speed vs distance")165st.pyplot(plt)166 167 168st.write("", divider=True)169st.markdown("""170```python171data=pd.DataFrame({"name":["google","alexa","google","alexa","google","alexa","google","alexa","google","alexa","google","alexa"],"month":[1,2,3,4,5,6,7,8,9,10,11,12],"sales":[12,34,12,34,12,45,4,6,7,9,12,13]})172 173google = data.groupby("name").get_group("google")174alexa = data.groupby("name").get_group("alexa")175 176fig=plt.figure(figsize=(3,4),facecolor="w")177axes=fig.add_axes([0,0,1,1]) # last,bottom,height,width178axes.plot(google["month"],google["sales"],marker="h",linestyle="--",markersize=3,linewidth=0.3)179axes.plot(alexa["month"],alexa["sales"],marker="o",linestyle="--",markersize=3,linewidth=0.3)180axes.set_xlabel("month")181axes.set_ylabel("sales")182 183axes.set_title("ave")184 185axes.legend()186st.pyplot(fig) ``` """)187data=pd.DataFrame({"name":["google","alexa","google","alexa","google","alexa","google","alexa","google","alexa","google","alexa"],"month":[1,2,3,4,5,6,7,8,9,10,11,12],"sales":[12,34,12,34,12,45,4,6,7,9,12,13]})188 189google = data.groupby("name").get_group("google")190alexa = data.groupby("name").get_group("alexa")191 192fig=plt.figure(figsize=(3,4),facecolor="w")193axes=fig.add_axes([0,0,1,1]) # last,bottom,height,width194axes.plot(google["month"],google["sales"],marker="h",linestyle="--",markersize=3,linewidth=0.3)195axes.plot(alexa["month"],alexa["sales"],marker="o",linestyle="--",markersize=3,linewidth=0.3)196axes.set_xlabel("month")197axes.set_ylabel("sales")198 199axes.set_title("ave")200 201axes.legend()202st.pyplot(fig)203 204 205 206st.write("", divider=True)207st.markdown("""208```python209 210data = pd.DataFrame({211 "name": ["google", "alexa", "google", "alexa", "google", "alexa", 212 "google", "alexa", "google", "alexa", "google", "alexa"],213 "month": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],214 "sales": [12, 34, 12, 34, 12, 45, 4, 6, 7, 9, 12, 13]215})216 217google = data.groupby("name").get_group("google")218alexa = data.groupby("name").get_group("alexa")219 220fig, axes = plt.subplots(figsize=(6, 4)) 221axes.plot(google["month"], google["sales"], marker="h", linestyle="--", 222 markersize=5, linewidth=1, label="Google") 223axes.plot(alexa["month"], alexa["sales"], marker="o", linestyle="--", 224 markersize=5, linewidth=1, label="Alexa") 225 226 227axes.set_xlabel("Month")228axes.set_ylabel("Sales")229axes.set_title("Sales Comparison between Google and Alexa")230axes.legend() 231 232plt.show()233st.pyplot(fig)234``` 235""")236data = pd.DataFrame({237 "name": ["google", "alexa", "google", "alexa", "google", "alexa", 238 "google", "alexa", "google", "alexa", "google", "alexa"],239 "month": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],240 "sales": [12, 34, 12, 34, 12, 45, 4, 6, 7, 9, 12, 13]241})242 243google = data.groupby("name").get_group("google")244alexa = data.groupby("name").get_group("alexa")245 246fig, axes = plt.subplots(figsize=(6, 4)) 247axes.plot(google["month"], google["sales"], marker="h", linestyle="--", 248 markersize=5, linewidth=1, label="Google") 249axes.plot(alexa["month"], alexa["sales"], marker="o", linestyle="--", 250 markersize=5, linewidth=1, label="Alexa") 251 252 253axes.set_xlabel("Month")254axes.set_ylabel("Sales")255axes.set_title("Sales Comparison between Google and Alexa")256axes.legend() 257 258plt.show()259st.pyplot(fig)260 261 262 263 264st.title("Mathematical Functions Visualization with Subplots")265st.write("", divider=True)266 267 268plt.figure(figsize=(15, 10)) 269 270 271st.header(" # 1. Linear Function Plot ")272st.markdown(""" Explanation:273- This subplot shows a linear relationship where y is equal to x. 274- It helps visualize the concept of linear functions clearly.275 276- **Type**: Single Subplot """)277st.markdown("""278```python279plt.subplot(2, 3, 1) 280plt.plot([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])281plt.title("Linear")282```283""")284 285plt.subplot(2, 3, 1) 286plt.plot([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])287plt.title("Linear")288 289 290st.header(" # 2. Exponential Function Plot ")291st.markdown(""" Explanation:292- This subplot depicts the exponential growth function, showing how y = e^x increases rapidly 293- as x increases, highlighting the nature of exponential functions.294 295- **Type**: Single Subplot """)296st.markdown("""297```python298plt.subplot(2, 3, 2) 299plt.plot([1, 2, 3, 4, 5, 6], np.exp([1, 2, 3, 4, 5, 6]))300plt.title("Exponential")301```302""")303 304plt.subplot(2, 3, 2) 305plt.plot([1, 2, 3, 4, 5, 6], np.exp([1, 2, 3, 4, 5, 6]))306plt.title("Exponential")307 308 309st.header(" # 3. Logarithmic Function Plot")310st.markdown(""" Explanation:311- This subplot illustrates the logarithmic function base 2. It shows how y increases slowly 312- as x increases, representing the diminishing returns of logarithmic growth.313 314- **Type**: Single Subplot """)315st.markdown("""316```python317plt.subplot(2, 3, 3) 318plt.plot([1, 2, 3, 4, 5, 6], np.log2([1, 2, 3, 4, 5, 6]))319plt.title("Log2")320```321""")322 323plt.subplot(2, 3, 3) 324plt.plot([1, 2, 3, 4, 5, 6], np.log2([1, 2, 3, 4, 5, 6]))325plt.title("Log2")326 327 328st.header(" # 4. Sine Function Plot ")329st.markdown(""" Explanation:330- This subplot represents the sine function, showcasing its periodic nature, 331- which is essential in understanding waveforms and oscillations.332 333- **Type**: Single Subplot """)334st.markdown("""335```python336plt.subplot(2, 3, 4) 337plt.plot([1, 2, 3, 4, 5, 6], np.sin([1, 2, 3, 4, 5, 6]))338plt.title("Sine")339```340""")341 342plt.subplot(2, 3, 4) 343plt.plot([1, 2, 3, 4, 5, 6], np.sin([1, 2, 3, 4, 5, 6]))344plt.title("Sine")345 346 347st.header(" # 5. Cosine Function Plot ")348st.markdown(""" Explanation:349- This subplot illustrates the cosine function, another periodic function, 350- demonstrating its relationship to the sine function and its application in wave mechanics.351 352- **Type**: Single Subplot """)353st.markdown("""354```python355plt.subplot(2, 3, 5) 356plt.plot([1, 2, 3, 4, 5, 6], np.cos([1, 2, 3, 4, 5, 6]))357plt.title("Cosine")358```359""")360 361plt.subplot(2, 3, 5) 362plt.plot([1, 2, 3, 4, 5, 6], np.cos([1, 2, 3, 4, 5, 6]))363plt.title("Cosine")364 365 366st.header(" # 6. Square Root Function Plot ")367st.markdown(""" Explanation:368- This subplot shows the square root function, which depicts the concept of roots 369- and is fundamental in various mathematical contexts.370 371- **Type**: Single Subplot """)372st.markdown("""373```python374plt.subplot(2, 3, 6) 375plt.plot([1, 2, 3, 4, 5, 6], np.sqrt([1, 2, 3, 4, 5, 6]))376plt.title("Square Root")377```378""")379 380plt.subplot(2, 3, 6) 381plt.plot([1, 2, 3, 4, 5, 6], np.sqrt([1, 2, 3, 4, 5, 6]))382plt.title("Square Root")383 384 385plt.tight_layout()386 387 388st.pyplot(plt)389 390 391 392 393 394 395 396 397 398 399 