Sunday, 3 September 2023

How to Add Labels to a Plot in Pyplot?

Using label argument of ‘plot’ method, we can add labels to a plot.

 

Example

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

 

These labels are used in plot legend, when there are multiple plots present in the same figure.

 

line_labels.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='solid-line', linestyle='-', marker='o')
plt.plot(x2, y2, color='g', label='dashed-line', linestyle='--', marker='s')
plt.plot(x3, y3, color='#0000FF', label='dotted-line', linestyle=':', marker='x')
plt.plot(x4, y4, color=(0, 1, 1), label='dashed-dotted-line', linestyle='-.', marker='+')

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

plt.show()

 

Output

 


 

Previous                                                    Next                                                    Home

A Guide to the Pandas Series Constructor

In this post, I am going to deep dive into the Pandas Series constructor.

 

Following table summarizes the parameters of Series constructor.

 

Parameter

Description

data

Specifies the data that we want to include in the Series. data can be a Python list, NumPy array, dictionary, scalar value, or another Series.

index

Index argument allows you to specify custom labels for the elements of the series.

dtype

Specify the data type of the series

copy

If copy is set to True, then Pandas create a Series from an existing object by copying the data. 'copy' argument is set to Tru by default.

 

If copy is set to False, then the original data is shared with Series object, If you modify the original object, the it modifies the Series.

name

Set the name to a Pandas Series.

 

data argument

We can specify the value to 'data' parameter directly in Series method.

series_from_list = pd.Series(data=[2, 3, 5, 7])

 

Mentioning the name ‘data’ is optional, following statement do the same.

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

data_argument.py

import pandas as pd
import numpy as np

# data can be a Python list, NumPy array, dictionary, scalar value, or another Series.
series_from_list = pd.Series([2, 3, 5, 7])
series_from_list_1 = pd.Series(data=[2, 3, 5, 7])
series_from_numpy_array = pd.Series(np.array([2, 3, 5, 7]))
series_from_dict = pd.Series({'FirstPrime' : 2, 'SecondPrime' : 3, 'ThirdPrime' : 5})
series_from_scalar = pd.Series(2)
series_from_another_series = pd.Series(series_from_list)

print('series_from_list:\n',series_from_list)
print('\nseries_from_list_1:\n',series_from_list_1)
print('\nseries_from_numpy_array:\n',series_from_numpy_array)
print('\nseries_from_dict:\n',series_from_dict)
print('\nseries_from_scalar:\n',series_from_scalar)
print('\nseries_from_another_series:\n',series_from_another_series)

Output

series_from_list:
 0    2
1    3
2    5
3    7
dtype: int64

series_from_list_1:
 0    2
1    3
2    5
3    7
dtype: int64

series_from_numpy_array:
 0    2
1    3
2    5
3    7
dtype: int64

series_from_dict:
 FirstPrime     2
SecondPrime    3
ThirdPrime     5
dtype: int64

series_from_scalar:
 0    2
dtype: int64

series_from_another_series:
 0    2
1    3
2    5
3    7
dtype: int64

index argument

Example

data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(data, index)

index_argument.py

import pandas as pd

data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series = pd.Series(data, index)

print(series)

Output

first_prime     2
second_prime    3
third_prime     5
fourth_prime    7
dtype: int64

dtype argument

Example 1: Data type is derived from the data by default

data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series1 = pd.Series(data, index)

In the above example, Pandas deduce data type as int64

first_prime     2
second_prime    3
third_prime     5
fourth_prime    7
dtype: int64 

Example 2: Specify the data type explicitly.

series2 = pd.Series(data, index, dtype='float64')

In the above example, I set the data type as float64 explicitly.

 

dtype_argument.py

import pandas as pd

data = [2, 3, 5, 7]
index = ['first_prime', 'second_prime', 'third_prime', 'fourth_prime']
series1 = pd.Series(data, index)
series2 = pd.Series(data, index, dtype='float64')

print(series1,'\n')
print(series2)

copy argument

If copy is set to True, then Pandas create a Series from an existing object by copying the data.

If copy is set to False, then the original data is shared with Series object, If you modify the original object, the it modifies the Series.


name argument

name_argument.py

import pandas as pd

series_1 = pd.Series([2, 3, 5, 7, 11], name='five_primes')
print(series_1)

Output

0     2
1     3
2     5
3     7
4    11
Name: five_primes, dtype: int64


Previous                                                 Next                                                 Home

Convert Pandas Series to NumPy Array in Python

 sing series ‘values’ attribute, we can get the array of values contained in the Series. The return type of values attribute is the NumPy array

 

get_series_values.py

import pandas as pd

primes = [2, 3, 5]
primes_index = ['first_prime', 'second_prime', 'third_prime']
primes_series = pd.Series(primes, index=primes_index, name='Hold the Prime numbers')

primes_series_values = primes_series.values

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

 

Output

primes_series_values :  [2 3 5]
type of primes_series_values :  <class 'numpy.ndarray'>

 

 

 

 

Previous                                                 Next                                                 Home

How to Split a PlantUML Diagram into Multiple Images?

page (hpages)x(vpages) command split the image into multiple pages.

 

hpages: Specifies number of horizontal pages

vpages: Specifies number of vertical pages.

 

splitDiagramIntoPages.txt

@startuml

	page 2x2

	skinparam pageMargin 10
	skinparam pageExternalColor gray
	skinparam pageBorderColor black

	package "java.lang" #lightblue {
		interface Appendable
		interface AutoCloseable
		interface Cloneable

		class Boolean
		class System
		class Object

		Object <|-- Boolean
		Object <|-- System

	}

	package "java.util" #F0F0F0{
		interface Collection<E>
		interface List<E>

		class ArrayList<E> implements List
		Object <|-- ArrayList
	}
	
@enduml

 

Above snippet generate 4 image files.

 

splitDiagramIntoPages.png   



splitDiagramIntoPages_001.png


 

splitDiagramIntoPages_002.png


 


splitDiagramIntoPages_003.png


 

 

Previous                                                    Next                                                    Home

Force Layout in PlantUML Diagrams with Hidden Links

‘hidden’ links can be used to force the layout.

For example, without hidden links.

 

withoutHiddenLinks.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

@enduml

 

Above snippet generate below diagram.

  


withHiddenLinks.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

Appendable -[hidden]--> Collection
Cloneable -[hidden]--> ArrayList

@enduml

 

Above snippet generate below diagram.


 

 

 

Previous                                                    Next                                                    Home

How to Group Classes Together in PlantUML Diagrams?

Using ‘together’ keyword, we can group some classes together.

together.txt

@startuml

together {
	interface Appendable
	interface Cloneable

        class System
}

together {
	interface Collection<E>
	interface List<E>

	class ArrayList<E> implements List
}

Appendable -[hidden]--> Collection

@enduml

 

‘hidden’ links can be used to force the layout.

 

Above snippet generate below diagram.



 

 

Previous                                                    Next                                                    Home