multiple values in scatter plot python

multiple values in scatter plot python

Creating a scatter plot with multiple Y values and categorical X values can be a bit challenging. Based on your example, I'll provide you with a few methods to achieve this using Python's Matplotlib library.

Method 1: Using Matplotlib and Pyplot

You're already familiar with the basic syntax of creating a scatter plot using Matplotlib and Pyplot. The main issue is that XVals contains categorical values (strings), while YVals contains numerical values. You can convert the categorical values to numerical indices, which can be used as x-values in the scatter plot.

Here's an example:

import matplotlib.pyplot as plt

# Convert categorical XVals to numerical indices
x_labels = ['10-Dec-18', '11-Dec-18']
x_indices = [i for i, _ in enumerate(x_labels)]

for y_vals in YVals:
 plt.scatter(x_indices, y_vals)

plt.xlabel('Day')
plt.ylabel('Value')
plt.show()

This method works by iterating over each set of Y values and plotting them against the numerical indices (0, 1, …). You can customize the appearance of the plot as needed.

Method 2: Using Seaborn's scatterplot

Seaborn is a Python data visualization library built on top of Matplotlib. It provides an elegant way to create informative and attractive statistical graphics.

import seaborn as sns
import matplotlib.pyplot as plt

sns.scatterplot(x=x_indices, y=YVals[0], hue='YVals')
plt.xlabel('Day')
plt.ylabel('Value')
plt.show()

This method creates a scatter plot with the numerical indices (x-axis) and the corresponding Y values. The hue parameter allows you to color the points based on the categorical X values.

Method 3: Using Plotly

Plotly is another popular Python data visualization library that allows for interactive plots.

import plotly.express as px

fig = px.scatter(x=x_indices, y=YVals[0])
fig.update_layout(title='Scatter Plot', xaxis_title='Day', yaxis_title='Value')
fig.show()

This method creates an interactive scatter plot with the numerical indices (x-axis) and the corresponding Y values.

Method 4: Custom Function with Matplotlib

You can create a custom function to plot multiple Y values using Matplotlib. This approach allows for more control over the plotting process.

import matplotlib.pyplot as plt

def multi_y_scatter(x, ys):
 for y in ys:
 plt.scatter(x, y)

x = x_indices
ys = [y_vals for y_vals in YVals]

multi_y_scatter(x, ys)
plt.xlabel('Day')
plt.ylabel('Value')
plt.show()

This method creates a scatter plot with multiple sets of Y values plotted against the numerical indices (x-axis).

Method 5: One-Liner with List Comprehension and Matplotlib

For a concise approach, you can use list comprehension to create a one-liner plotting function.

import matplotlib.pyplot as plt

[x] + [plt.scatter([x], y) for y in YVals[0]]
plt.xlabel('Day')
plt.ylabel('Value')
plt.show()

This method creates a scatter plot with multiple sets of Y values plotted against the numerical indices (x-axis).

I hope these methods help you create the desired scatter plot!