Issue
I want to draw a scatter plot as shown in the figure but it seems that my code is not working the same as required, I am a begginer in python with not having much experience.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Production_steps = [13,12,21,1,29,20,15,12,21,18,20,15,20,22,26,7,8,12,22,25,15,23,24,13,4,22,12]
Step_name = ['Start','NA2S','waiting','NA+','vacume','NA-','90min_Wait','waiting','NA+','waiting','vacume','vacume','1h_wait','Pressue','vacume','vacume','NA+/-','NA+','Pressure','90min_Wait','Pressure','cool-','vacume','vacume','NA-','Pressure','Batch Start']
Parameter1 = [80,932,21,525,20,20,21,1090,19,60,18,33,19,936,1083,61,102,1088,935,38,20,83,21,79,1006,930,1088]
# Create a DataFrame using the given data
df = pd.DataFrame({'Production_steps': Production_steps,
'Step_name': Step_name, 'Parameter1': Parameter1})
# Get unique values of 'Step_name'
unique_steps = df['Step_name'].unique()
# Set up colors for each unique 'Step_name'
colors = plt.cm.rainbow(np.linspace(0, 1, len(unique_steps)))
# Create a figure and axis
fig, ax = plt.subplots(figsize=(13, 6))
# Loop through unique 'Step_name' values, filter data, and plot scatter points
for i, step in enumerate(unique_steps):
step_data = df[df['Step_name'] == step]
ax.scatter(step_data['Production_steps'],step_data['Parameter1'], label=step, color=colors[i])
# Set labels and show legend
ax.set_xlabel('Production_steps')
ax.set_ylabel('Parameter1')
# Create legend outside of plot to position it at the bottom
fig.legend(loc='lower center', ncol=4, bbox_to_anchor=(0.5,-0.2))
# Show the scatter plot
plt.show()
Solution
The following code works perfeectly for my question.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Production_steps = [13,12,21,1,29,20,15,12,21,18,20,15,20,22,26,7,8,12,22,25,15,23,24,13,4,22,12]
Step_name = ['Start','NA2S','waiting','NA+','vacume','NA-','90min_Wait','waiting','NA+','waiting','vacume','vacume','1h_wait','Pressue','vacume','vacume','NA+/-','NA+','Pressure','90min_Wait','Pressure','cool-','vacume','vacume','NA-','Pressure','Batch Start']
Parameter1 = [80,932,21,525,20,20,21,1090,19,60,18,33,19,936,1083,61,102,1088,935,38,20,83,21,79,1006,930,1088]
# Create a DataFrame using the given data
df = pd.DataFrame({'Production_steps': Production_steps,
'Step_name': Step_name, 'Parameter1': Parameter1})
# Get unique values of 'Step_name'
unique_steps = df['Step_name'].unique()
# Set up colors for each unique 'Step_name'
colors = plt.cm.rainbow(np.linspace(0, 1, len(unique_steps)))
# Create a figure and axis
fig, ax = plt.subplots(figsize=(13, 6))
# Loop through unique 'Step_name' values, filter data, and plot scatter points
for i, step in enumerate(unique_steps):
step_data = df[df['Step_name'] == step]
ax.scatter(step_data['Production_steps'],step_data['Parameter1'], label=step, color=colors[i])
# Set labels and show legend
ax.set_xlabel('Production_steps')
ax.set_ylabel('Parameter1')
# Create legend outside of plot to position it at the bottom
fig.legend(loc='lower center', ncol=4, bbox_to_anchor=(0.5,-0.2))
# Show the scatter plot
plt.show()
Answered By - wasif ahmed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.