Home > front end >  Multiplot of a parallel coordinate in python
Multiplot of a parallel coordinate in python

Time:12-19

I have a dataframe with stats from Pokemon and try to compare the stats from the different types. I want to show it in a parallel coordinate chart with 2 different types in a multiplot.
Actually I'm using parallel_coordinates() from pandas for the chart and tried to use plt.subplot() from matplotlib for the multiplot. The problem is I cant declare in what axes I want to draw the values. Is it possible to draw with pandas a multiplot too? Or can I use the matplotlib for this?

This was my first attempt:

import pandas as pd
import matplotlib.pyplot as plt

values = \['Attack', 'Defense', 'HP', 'Sp.Attack', 'Sp.Defense', 'Speed'\]

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15,5))
fig.tight_layout()

df_fireWater = df_pokemon\[(df_pokemon\['Primary Type'\] == 'FIRE') | (df_pokemon\['Primary Type'\] == 'WATER')\]
ax1 = pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('red', 'blue'))

df_waterPsychic = df_pokemon\[(df_pokemon\['Primary Type'\] == 'WATER') | (df_pokemon\['Primary Type'\] == 'PSYCHIC')\]
ax2 = pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('blue', 'violet'))

df_waterRock = df_pokemon\[(df_pokemon\['Primary Type'\] == 'WATER') | (df_pokemon\['Primary Type'\] == 'ROCK')\]
ax3 = pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('blue', 'brown'))

When I run my code everything will be drawn in the last chart and the first two are empty. Does anyone know how I can solve my problem?

CodePudding user response:

To create a multi-plot figure with pandas parallel_coordinates function, you can use the subplots function from matplotlib to create a figure with multiple subplots and use the ax parameter of the parallel_coordinates function to specify which subplot you want to draw in.

Here's an example of how you can modify your code to create a multi-plot figure:

import pandas as pd
import matplotlib.pyplot as plt

# Create a figure with 3 subplots
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15,5))
fig.tight_layout()

values = ['Attack', 'Defense', 'HP', 'Sp.Attack', 'Sp.Defense', 'Speed']

# Draw the first plot in ax1
df_fireWater = df_pokemon[(df_pokemon['Primary Type'] == 'FIRE') | (df_pokemon['Primary Type'] == 'WATER')]
pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('red', 'blue'), ax=ax1)

# Draw the second plot in ax2
df_waterPsychic = df_pokemon[(df_pokemon['Primary Type'] == 'WATER') | (df_pokemon['Primary Type'] == 'PSYCHIC')]
pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('blue', 'violet'), ax=ax2)

# Draw the third plot in ax3
df_waterRock = df_pokemon[(df_pokemon['Primary Type'] == 'WATER') | (df_pokemon['Primary Type'] == 'ROCK')]
pd.plotting.parallel_coordinates(df_fireWater, 'Primary Type', values, color=('blue', 'brown'), ax=ax3)

This should create a figure with three subplots, each containing a parallel coordinate plot comparing the stats of the specified Pokemon types.

  • Related