0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Data Visualization with pandas

0
Last updated at Posted at 2020-12-31

Bar plot

Bar plots are useful to visualize relationship between a categorical and numerical variables.

# Import matplotlib.pyplot with alias plt
import matplotlib.pyplot as plt

# Look at the first few rows of data
print(avocados.head())

# Get the total number of avocados sold of each size
nb_sold_by_size = avocados.groupby('size')['nb_sold'].sum()

# Create a bar plot of the number of avocados sold by size
nb_sold_by_size.plot(kind="bar")

# Show the plot
plt.show()

Line plots

Line plots are great to visualize change in numerical variable such as sales over time

# Import matplotlib.pyplot with alias plt
import matplotlib.pyplot as plt

# Get the total number of avocados sold on each date
nb_sold_by_date = avocados.groupby('date')['nb_sold'].sum()

# Create a line plot of the number of avocados sold by date
nb_sold_by_date.plot(kind="line")

# Show the plot
plt.show()

Scatter plots

Scatter plots help to visualize relationship between two numerical variables.

# Scatter plot of nb_sold vs avg_price with title
avocados.plot(kind="scatter",x="nb_sold",y="avg_price",title="Number of avocados sold vs. average price")

# Show the plot
plt.show()

Layering histograms on top of each other

# Modify bins to 20
avocados[avocados["type"] == "conventional"]["avg_price"].hist(bins=20,alpha=0.5)

# Modify bins to 20
avocados[avocados["type"] == "organic"]["avg_price"].hist(bins=20,alpha=0.5)

# Add a legend
plt.legend(["conventional", "organic"])

# Show the plot
plt.show()

Heatmap with seaborn

Let's say you have 2 dimensional numpy array V_result and you want to plot it as a heat map

import seaborn as sns
import matplotlib.pyplot as plt

states_row_length = 20
states_col_length = 20
V_result = np.zeros((states_row_length, states_col_length))
for row in range(states_row_length):
    for col in range(states_col_length):
        V_result[row, col] = state_values[State(row + 1, col + 1)]
sns.heatmap(V_result, annot=True, linewidths=2, vmin=0, vmax=20, cmap=sns.color_palette("Reds", 24))
plt.title("Optimal state values")
plt.show()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?