Plot 3 categorical variables python

Note

Click here to download the full example code

You can pass categorical values [i.e. strings] directly as x- or y-values to many plotting functions:

import matplotlib.pyplot as plt

data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}
names = list[data.keys[]]
values = list[data.values[]]

fig, axs = plt.subplots[1, 3, figsize=[9, 3], sharey=True]
axs[0].bar[names, values]
axs[1].scatter[names, values]
axs[2].plot[names, values]
fig.suptitle['Categorical Plotting']

Text[0.5, 0.98, 'Categorical Plotting']

This works on both axes:

cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"]

fig, ax = plt.subplots[]
ax.plot[activity, dog, label="dog"]
ax.plot[activity, cat, label="cat"]
ax.legend[]

plt.show[]

Gallery generated by Sphinx-Gallery

Categorical data can we visualized using two plots, you can either use the functions pointplot[], or the higher-level function factorplot[].

Factorplot

Factorplot draws a categorical plot on a FacetGrid. Using ‘kind’ parameter we can choose the plot like boxplot, violinplot, barplot and stripplot. FacetGrid uses pointplot by default.

Example

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset['exercise']
sb.factorplot[x = "time", y = pulse", hue = "kind",data = df];
plt.show[]

Output

We can use different plot to visualize the same data using the kind parameter.

Example

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset['exercise']
sb.factorplot[x = "time", y = "pulse", hue = "kind", kind = 'violin',data = df];
plt.show[]

Output

In factorplot, the data is plotted on a facet grid.

What is Facet Grid?

Facet grid forms a matrix of panels defined by row and column by dividing the variables. Due of panels, a single plot looks like multiple plots. It is very helpful to analyze all combinations in two discrete variables.

Let us visualize the above the definition with an example

Example

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset['exercise']
sb.factorplot[x = "time", y = "pulse", hue = "kind", kind = 'violin', col = "diet", data = df];
plt.show[]

Output

The advantage of using Facet is, we can input another variable into the plot. The above plot is divided into two plots based on a third variable called ‘diet’ using the ‘col’ parameter.

We can make many column facets and align them with the rows of the grid −

Example

import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
df = sb.load_dataset['titanic']
sb.factorplot["alive", col = "deck", col_wrap = 3,data = df[df.deck.notnull[]],kind = "count"]
plt.show[]

output

How do you visualize 3 variables?

To graph three variables, the best choice is clustered bar chart. We can graph three variables using many programs such as Excel, power point etc. A line graph is a graphical representation of data that changes over a period of time. It consists of a horizontal x-axis and a vertical y-axis.

How do you plot two categorical variables in Python?

MatPlotLib with Python.
Set the figure size and adjust the padding between and around the subplots..
Create a dictionary with some details..
Extract the keys and values from the dictionary [Step 2]..
Create a figure and a set of subplots..
Plot bar, scatter and plot with names and values data..

Which plot is best for categorical variables in Python?

Barplot. A barplot is basically used to aggregate the categorical data according to some methods and by default its the mean.

How do you visualize 2 categorical variables?

Data concerning two categorical [i.e., nominal- or ordinal-level] variables can be displayed in a two-way contingency table, clustered bar chart, or stacked bar chart.

Bài mới nhất

Chủ Đề