Other charts

admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: Other charts

Post by admin »

Python: Dual Axis Line Chart

A dual-axis chart allows plotting two datasets with different scales on the same chart —
one on the primary Y-axis, and the other on a secondary Y-axis.

Example – Student height and weight:

Code: Select all

import pandas as pd
import matplotlib.pyplot as plt

dataset = pd.DataFrame({
    'Name': ['Rohit', 'Seema', 'Meena', 'Geeta', 'Rajat'],
    'Height': [155, 129, 138, 164, 145],
    'Weight': [60, 40, 45, 55, 60]
})

# Primary Y-axis plot (Height)
ax = dataset.plot(kind='line', x='Name', y='Height', color='Blue', linewidth=3)

# Secondary Y-axis plot (Weight)
ax2 = dataset.plot(kind='line', x='Name', y='Weight', secondary_y=True,
                   color='Red', linewidth=3, ax=ax)

plt.title("Student Data")

# Axis labels
ax.set_xlabel('Name', color='g')
ax.set_ylabel('Height', color='b')
ax2.set_ylabel('Weight', color='r')

plt.tight_layout()
plt.show()
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: Other charts

Post by admin »

Python: Pareto Chart

A Pareto Chart is a bar chart combined with a cumulative percentage line —
useful for identifying the most important factors in a dataset.

Example:

Code: Select all

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter

df = pd.DataFrame({'country': [177.0, 7.0, 4.0, 2.0, 1.0, 0.8, 0.4, 0.2, 0.1]})
df.index = ['USA', 'Canada', 'Russia', 'UK', 'Belgium', 'Mexico', 'Germany', 'Denmark', 'India']
df = df.sort_values(by='country', ascending=False)
df['cumpercentage'] = df['country'].cumsum() / df['country'].sum() * 100

fig, ax = plt.subplots()
ax.bar(df.index, df['country'], color="blue")
ax2 = ax.twinx()
ax2.plot(df.index, df['cumpercentage'], color="red", marker="o")
ax2.yaxis.set_major_formatter(PercentFormatter())

ax.tick_params(axis='y', colors="blue")
ax2.tick_params(axis='y', colors="red")

plt.show()
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: Other charts

Post by admin »

Python: Seasonal Decomposition

Decomposes a time series into:
- Level (average value)
- Trend (long-term increase/decrease)
- Seasonality (repeating short-term cycle)
- Noise (random variation)

Example:

Code: Select all

from statsmodels.tsa.seasonal import seasonal_decompose
import pandas as pd

df = pd.read_csv('AirPassengers.csv')  # Monthly airline passenger counts
seasonal_decompose(df, period=12).plot()
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: Other charts

Post by admin »

Python: Custom Line Plot

Example showing a quadratic curve with dashed lines, markers, and custom axes.

Code: Select all

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9]
y = [1,4,9,16,25,36,49,64,81]

plt.figure(figsize=(4,2.25))
plt.plot(x, y, label='Quadratic Curve', color='blue', marker='o',
         linestyle='dashed', linewidth=2, markersize=5)

plt.xlabel('X')
plt.ylabel('Y')
plt.title('X vs Y')
plt.legend(loc='best')
plt.xticks(rotation=45)
plt.yticks(rotation=45)
plt.xlim(-2, 12)
plt.ylim(-10, 100)
plt.show()
admin
Site Admin
Posts: 119
Joined: Fri May 10, 2024 2:46 pm
Location:

Re: Other charts

Post by admin »

Python: Annotated Plot

Annotations are useful for adding explanations to data points.

Code: Select all

import matplotlib.pyplot as plt

plt.figure(figsize=(5,4.5))
plt.plot([1,2,3], [1,4,9], color='blue', linestyle='dashed', marker='o')

annotation = 'This is an annotation used for briefing the plot'
plt.annotate(annotation, xy=(2,4), xytext=(3.5,5),
             arrowprops=dict(facecolor='blue', shrink=0.05))

plt.show()
Post Reply