Thursday, 7 September 2023

How to Customize line thickness in PlantUML class diagram?

Class1 -[thickness=1]-> Class3

 

Above snippet set the line thickness to 1 pixel.

 

lineThickness.txt

@startuml

class Class1
class Class2

Class1 --> Class2
Class1 -[thickness=1]-> Class3
Class1 -[thickness=2]-> Class4
Class1 -[thickness=4]-> Class5
Class1 -[thickness=8]-> Class6
Class1 -[thickness=16]-> Class7

@enduml

  Above snippet generate below diagram.

 


We can use line style, color and thickness all together.

Class1 -[#red,bold,thickness=1]-> Class3

lineStyleColorThickness.txt

@startuml

class Class1
class Class2

Class1 --> Class2
Class1 -[#red,bold,thickness=1]-> Class3
Class1 -[#blue,dashed,thickness=2]-> Class4
Class1 -[#green,dotted,thickness=4]-> Class5 
Class1 -[#0000ff,hidden,thickness=8]-> Class6
Class1 -[#gold,plain,thickness=16]-> Class7

@enduml

Above snippet generate below diagram.


  


Previous                                                    Next                                                    Home

Convert Pandas Series to Dictionary

‘dict’ method takes a series object and return the dictionary. The resulting dictionary have series index values as key and corresponding values as dictionary values.

 

Example 1: With default numeric index

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

 

‘primes_dict’ contain below values.

{0: 2, 1: 3, 2: 5, 3: 7}

 

Example 2: With index labels.
index_labels = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(primes, index_labels)
primes_dict = dict(series)

 

‘primes_dict’ contain the below data.

{'first_prime': 2, 'second_prime': 3, 'third_prime': 5, 'fourth_prime': 7}

 

convert_list_to_dictionary.py

import pandas as pd

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

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

primes_dict = dict(series)
print('primes_dict : ', primes_dict)

index_labels = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(primes, index_labels)

print('\nOriginal Data : ')
print(series)

primes_dict = dict(series)
print('primes_dict : ', primes_dict)

 

Output

Original Data : 
0    2
1    3
2    5
3    7
dtype: int64
primes_dict :  {0: 2, 1: 3, 2: 5, 3: 7}

Original Data : 
first_prime     2
second_prime    3
third_prime     5
fourth_prime    7
dtype: int64
primes_dict :  {'first_prime': 2, 'second_prime': 3, 'third_prime': 5, 'fourth_prime': 7}

 

 

Previous                                                 Next                                                 Home

How to Customize Legend Location in Pyplot?

Using 'loc' parameter of legend method, we can customize the legend location.

 

Example 1: Legend location is at 'upper right'.

plt.legend(loc='upper right')



Example 2: Legend location is at 'upper left'.

plt.legend(loc='upper left')


 

Example 3: Legend location is at 'lower left'.

plt.legend(loc='lower left')


Example 4: Legend location is at 'lower right'.

plt.legend(loc='lower right')


Find the below working application.

 

legend_location_customization.py

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 10)
y1 = x**2
y2 = x**3

plt.plot(x, y1, label="y = x^2", color='r')
plt.plot(x, y2, label="y = x^3", color='g')

plt.legend(loc='upper left')
plt.show()


Previous                                                    Next                                                    Home

Tuesday, 5 September 2023

Python built-in functions to work with series data

Following table summarizes the Python built-in functions that can be used with series data.

 

Functions

Description

dict

Get the dictionary from series data

dir

Get all the available attributes and methods available in series object

len

Get number of elements in a series

list

Get the list from series data

max

Get the maximum value in the series

min

Get the minimum value in the series

sorted

Sort the values of series

type

Tells whether given variable is a series object or not.

 

Previous                                                 Next                                                 Home

Pandas: Get the last n elements of the series

Using tail method, we can get the rows from the series end

 

Example

series.tail() : Return last 5 rows in the series.
series.tail(n) : Return last n rows in the series.

 

Find the below working application.

 

tail.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)

last_five_primes = series.tail()
last_three_primes = series.tail(3)

print('\nlast_five_primes')
print(last_five_primes)

print('\nlast_three_primes')
print(last_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

last_five_primes
6     17
7     19
8     23
9     29
10    31
dtype: int64

last_three_primes
8     23
9     29
10    31
dtype: int64

‘tail()’ method is a preview on original dataset, if you update something on the result returned by tail() method, it updates the original data. You can confirm the same from below application.

 

tail.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)

last_five_primes = series.tail()
last_three_primes = series.tail(3)

print('\nlast_five_primes')
print(last_five_primes)

print('\nlast_three_primes')
print(last_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

last_five_primes
6     17
7     19
8     23
9     29
10    31
dtype: int64

last_three_primes
8     23
9     29
10    31
dtype: int64

 

Previous                                                 Next                                                 Home

How Legends Make Your Plots More Informative?

 

Legend provide information about the various elements in the plot. Legends make the plot more informative to the end-user.

 

For example, take a look at the below diagram without legend.




From the above diagram, it is very difficult or impossible to the end user, what information the plot is telling to us.

 

With legend

 


As you observe above diagram with legend, it is very clear that

a.   Red color line represent the relationship between x and its square

b.   Green color line represent the relationship between x and its cube.

 

plot_legend.py

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(1, 10)
y1 = x**2
y2 = x**3

plt.plot(x, y1, label="y = x^2", color='r')
plt.plot(x, y2, label="y = x^3", color='g')

plt.legend()
plt.show()

 

 

Previous                                                    Next                                                    Home

How to Customize Line Color in PlantUML Class Diagrams

Class1 -[#red]-> Class3

 

Above statement applies red color to the connecting line between Class1 and Class3.

 

lineColor.txt

@startuml

class Class1
class Class2

Class1 --> Class2
Class1 -[#red]-> Class3
Class1 -[#blue]-> Class4
Class1 -[#green]-> Class5 
Class1 -[#0000ff]-> Class6
Class1 -[#gold]-> Class7

@enduml

 

Above snippet generate below diagram.

 


 

We can combine line color and line style at the same time.

Class1 -[#red,bold]-> Class3

 

lineColorAndStyle.txt

@startuml

class Class1
class Class2

Class1 --> Class2
Class1 -[#red,bold]-> Class3
Class1 -[#blue,dashed]-> Class4
Class1 -[#green,dotted]-> Class5 
Class1 -[#0000ff,hidden]-> Class6
Class1 -[#gold,plain]-> Class7

@enduml

 

Above snippet generate below diagram.

 



  

Previous                                                    Next                                                    Home