First up is Matplotlib it lets you create basic plots with just a few lines of code. For example:
# Importing the matplotlib library and assigning it an alias "plt"
import matplotlib.pyplot as plt
# Creating two lists, x and y, to store the data points for the plot
x = [1, 2, 3] # x-axis values
y = [5, 7, 9] # y-axis values
# Using the "plot" function from the matplotlib library to create a line graph with the data points from x and y
plt.plot(x, y) # creates a line graph with x and y values
# Using the "show" function from the matplotlib library to display the plot in a browser window
plt.show() # displays the plot in a browser window
Next is seaborn it’s like Matplotlib on juice! It has pre-defined styles and functions that make creating fancy plots super easy:
# Import the seaborn library
import seaborn as sns
# Load the dataset into a pandas DataFrame
data = pd.read_csv('your_dataset.csv')
# Create a bar plot using seaborn's barplot function
# Specify the x and y variables, as well as the data source
sns.barplot(x='column1', y='column2', data=data)
# Display the plot in a browser window
plt.show()
Bokeh is another cool library that lets you create interactive plots with just a few lines of code:
# Import the necessary module from Bokeh library
from bokeh.plotting import figure, output_file, show
# Specify the output file name for the plot
output_file('your_plot.html')
# Create a new plot object with a title and axis labels
p = figure(title='Your Plot', x_axis_label='X Label', y_axis_label='Y Label')
# Add a line to the plot using the specified x and y coordinates
p.line([1,2,3], [5,7,9])
# Display the plot in a browser window
show(p)
Finally, we have Altair and Plotly they’re both super powerful libraries that let you create complex plots with just a few lines of code:
# Importing the necessary libraries
import altair as alt # Importing Altair library and assigning it an alias "alt"
from vega_datasets import data # Importing data from Vega Datasets library
# Loading stock market data into a Vega-Lite source object
source = data.stocks()
# Creating a new chart using Altair's Chart function and assigning it to the variable "chart"
chart = alt.Chart(source)
# Adding a line mark to the chart using the mark_line() function
# and encoding the x-axis with the date column and the y-axis with the close column
chart = chart.mark_line().encode(x='date:T', y='close')
# Displaying the chart in the browser window
chart.display()
Whether you need basic plots or complex dashboards, these tools have got you covered!