Tuesday, 5 September 2023

PlantUML: How to Define the Implements Relationship?

Using ‘implements’ keyword, we can define the implements relationship.

implements.txt

@startuml

interface List{

}

class AbstractList implements List{

}

class ArrayList extends AbstractList{

}

class LinkedList extends AbstractList{

}
	
@enduml

 

Above snippet generate below diagram.


 

Previous                                                    Next                                                    Home

plot.show(): The Essential Step for Displaying Matplotlib Plots

When you define plot using Matplotlib library, those plot details are like lines, labels, colours, titles etc., are stored in in-memory, but these are not shown in the screen. To show the plot visually on the screen, you should call plot.show method.

 

plot_show_method.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

# Create a new figure with a specific size
plt.figure(figsize=(8, 6))

plt.plot(x1, y1, color='red')

plt.show()

 

Output

 

 


When ‘plot.show()’ method gets executed, it renders the plot visually on a window and make it visible on the screen.

 

Note

In case of interactive IDEs like Jupyter notebook, plot might be disaplyed without calling show method, but if you are running in standalone environment, then calling plot.show() method is mandatory to see the plot visually.


 

Previous                                                    Next                                                    Home

Pandas: Get the First n Elements of a Series

Using 'head()' method we can get 'n' number of rows from the dataset.

 

Example

head() : Get 5 rows from the beginning
head(n) : Get n rows from the beginning

 

head.py

import pandas as pd

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
series = pd.Series(primes)

print('Original Data : ')
print(series)

first_five_primes = series.head()
first_three_primes = series.head(3)

print('\nfirst_five_primes')
print(first_five_primes)

print('\nfirst_three_primes')
print(first_three_primes)

 

Output

Original Data : 
0      2
1      3
2      5
3      7
4     11
5     13
6     17
7     19
8     23
9     29
10    31
dtype: int64

first_five_primes
0     2
1     3
2     5
3     7
4    11
dtype: int64

first_three_primes
0    2
1    3
2    5
dtype: int64

‘head()’ method is a preview on original dataset, if you update something on the result returned by head() method, it updates the original data.



 

head_preview_update.py

import pandas as pd

primes = [2, 3, 5, 7]
series = pd.Series(primes)

print('Original Data : ')
print(series)

print('\nUpdating 1st prime to 11')
first_two_primes = series.head(2)
first_two_primes[0] = 11

print('\nAfter updating\n')
print(series)

Output

Original Data : 
0    2
1    3
2    5
3    7
dtype: int64

Updating 1st prime to 11

After updating

0    11
1     3
2     5
3     7
dtype: int64

 

Previous                                                 Next                                                 Home

Monday, 4 September 2023

How to convert a single column DataFrame to Series in Pandas

'squeeze()' method convert a one-column data frame to series.

 

Example

country_dataset = {
    'countries' : ['India', 'China', 'Sri Lanka']
}

df = pd.DataFrame(country_dataset)
series = df.squeeze()

 

Above snippet gets the series from one-column DataFrame.

 

dataframe_to_series.py

import pandas as pd

country_dataset = {
    'countries' : ['India', 'China', 'Sri Lanka']
}

df = pd.DataFrame(country_dataset)
series = df.squeeze()

print('df : ')
print(df)
print('type of df : ', type(df))

print('\nseries :')
print(series)
print('type of series : ', type(series))

 

Output

df : 
   countries
0      India
1      China
2  Sri Lanka
type of df :  <class 'pandas.core.frame.DataFrame'>

series :
0        India
1        China
2    Sri Lanka
Name: countries, dtype: object
type of series :  <class 'pandas.core.series.Series'>

If the dataframe has index labels, then same are used as row labels in a series.

 

series_from_dataframe_index.py

import pandas as pd

country_dataset = {
    'countries' : ['India', 'China', 'Sri Lanka'],
    'capitals' : ['New Delhi', 'Beijing', 'Colombo, Sri Jayawardenepura Kotte']
}

df = pd.DataFrame(country_dataset)
df.set_index('countries', inplace=True)
series = df.squeeze()

print('df : ')
print(df)
print('type of df : ', type(df))

print('\nseries :')
print(series)
print('type of series : ', type(series))

Output

df : 
                                     capitals
countries                                    
India                               New Delhi
China                                 Beijing
Sri Lanka  Colombo, Sri Jayawardenepura Kotte
type of df :  <class 'pandas.core.frame.DataFrame'>

series :
countries
India                                 New Delhi
China                                   Beijing
Sri Lanka    Colombo, Sri Jayawardenepura Kotte
Name: capitals, dtype: object
type of series :  <class 'pandas.core.series.Series'>

 

Previous                                                 Next                                                 Home

How to Control the Size of Markers in a Plot?

Using ‘markersize’ argument of plot method, we can control the size of markers in a plot.

 

Example

plt.plot(x1, y1, color='red', label='markersize-5', linestyle='-', marker='o', markersize=5)

Find the below working application.

 

marker_size.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

x3 = [15, 30, 45, 60, 75]
y3 = [8, 16, 24, 32, 40]

x4 = [12, 24, 36, 48, 60]
y4 = [15, 30, 45, 60, 75]

# Create a new figure with a specific size
plt.figure(figsize=(8, 6))

plt.plot(x1, y1, color='red', label='markersize-5', linestyle='-', marker='o', markersize=5)
plt.plot(x2, y2, color='g', label='markersize-10', linestyle='--', marker='o', markersize=10)
plt.plot(x3, y3, color='#0000FF', label='markersize-15', linestyle=':', marker='o', markersize=15)
plt.plot(x4, y4, color=(0, 1, 1), label='markersize-20', linestyle='-.', marker='o', markersize=20)

# To display the legend on the plot
plt.legend(fontsize=12)

plt.show()

Output




 

Previous                                                    Next                                                    Home

Using the extends keyword to specify class extension relationships in PlantUML

Using ‘extends’ keyword, we can specify the extension relationship.

extends.txt

@startuml

interface List{

}

class AbstractList implements List{

}

class ArrayList extends AbstractList{

}

class LinkedList extends AbstractList{

}
	
@enduml

 

Above snippet generate below diagram.

 


 

Previous                                                    Next                                                    Home

Sunday, 3 September 2023

How to Control the Width of the Lines in a Line Plot?

Using 'linewidth' argument of plot method, we can control the width of the lines in a line plot. Default value of linewidth is 1.0, increasing it makes the line thicker, while decreasing it makes the line thinner.

 

line_width.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

x3 = [15, 30, 45, 60, 75]
y3 = [8, 16, 24, 32, 40]

x4 = [12, 24, 36, 48, 60]
y4 = [15, 30, 45, 60, 75]

plt.plot(x1, y1, color='red', label='width-1.0', linestyle='-', marker='o', linewidth=1.0)
plt.plot(x2, y2, color='g', label='width-2.5', linestyle='--', marker='s', linewidth=2.5)
plt.plot(x3, y3, color='#0000FF', label='width-5.0', linestyle=':', marker='x', lw=5.0)
plt.plot(x4, y4, color=(0, 1, 1), label='width-7.5', linestyle='-.', marker='+', lw=7.5)

# To display the legend on the plot
plt.legend()

plt.show()

Output



 

 

Previous                                                    Next                                                    Home