Sunday, May 14, 2023

Python Commands for Data Visualization

Python provides several powerful libraries for data visualization. Here are some commonly used Python libraries along with example commands to perform data visualization:

1. Matplotlib: Matplotlib is a versatile plotting library that provides a wide range of visualization options.

   import matplotlib.pyplot as plt

   # Line plot

   x = [1, 2, 3, 4, 5]

   y = [1, 4, 9, 16, 25]

   plt.plot(x, y)

   plt.xlabel('X-axis')

   plt.ylabel('Y-axis')

   plt.title('Line Plot')

   plt.show()

 

  # Bar plot

   labels = ['A', 'B', 'C']

   values = [10, 15, 7]

   plt.bar(labels, values)

   plt.xlabel('Categories')

   plt.ylabel('Values')

   plt.title('Bar Plot')

   plt.show()


  2. Seaborn: Seaborn is a statistical data visualization library built on top of Matplotlib. It provides a high-level interface for creating attractive and informative visualizations.

   import seaborn as sns

   # Scatter plot

   tips = sns.load_dataset('tips')

   sns.scatterplot(data=tips, x='total_bill', y='tip', hue='smoker')

   plt.xlabel('Total Bill')

   plt.ylabel('Tip')

   plt.title('Scatter Plot')

   plt.show()


  

 

 # Box plot

   sns.boxplot(data=tips, x='day', y='total_bill')

   plt.xlabel('Day')

   plt.ylabel('Total Bill')

   plt.title('Box Plot')

   plt.show()


3. Plotly: Plotly is an interactive plotting library that allows you to create interactive and dynamic visualizations.

   import plotly.graph_objects as go

   # Scatter plot

   fig = go.Figure(data=go.Scatter(x=[1, 2, 3, 4, 5], y=[1, 4, 9, 16, 25]))

   fig.update_layout(title='Scatter Plot', xaxis_title='X-axis', yaxis_title='Y-axis')

   fig.show()

 # Heatmap

   z = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

   fig = go.Figure(data=go.Heatmap(z=z))

   fig.update_layout(title='Heatmap')

   fig.show()

 



4. Pandas: Pandas is a powerful data analysis library that includes built-in visualization capabilities.

   import pandas as pd

   # Line plot

   df = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [1, 4, 9, 16, 25]})

   df.plot(x='x', y='y', kind='line')

   plt.xlabel('X-axis')

   plt.ylabel('Y-axis')

   plt.title('Line Plot')

   plt.show()

   


# Histogram

   df.plot(kind='hist');

   plt.xlabel('Values')

   plt.ylabel('Frequency')

   plt.title('Histogram')

   plt.show()




These are just a few examples of the vast possibilities for data visualization in Python. Each library offers a wide range of customization options, so you can tailor your visualizations to your specific needs.

No comments:

Post a Comment

Clustering in Machine Learning

Clustering is a type of unsupervised learning in machine learning where the goal is to group a set of objects in such a way that objects in...