Issue
im trying to draw a straight horizontal line across all the mean of data points of the blue and red color.
and this is my code for the above picture
fig=plt.figure(figsize=(20,10), dpi=100)
ax=fig.add_axes([0,0,1,1])
ax.scatter(x='Speed', y='Port ME Homemade Mass Flow Rate (kg/s)', data=df1before, color='r', marker='1')
ax.scatter(x='Speed', y='Port ME Homemade Mass Flow Rate (kg/s)', data=df1after, color='b', marker='x')
ax.set_xlabel('Speed(Knot)')
ax.set_ylabel('Port ME Homemade Mass Flow Rate (kg/s)')
#plt.plot(x=df3[len('Speed')], y=df3['Port ME Homemade Mass Flow Rate (kg/s)'].mean())
plt.show()
The comment is what I have tried.. The desired output would be:
It would also be great if each line could show the value of position Y that it's at.
Thanks!
Solution
ax.axhline
draws a horizontal line at a given height, default going over the full width of the plot.
ax.text
puts a text at a given position. Using the y-axis transform, the x-coordinate can be given as a position relative to the axes, and the y-coordinate as a data value.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
y_name = 'Port ME Homemade Mass Flow Rate (kg/s)'
df1before = pd.DataFrame({'Speed': np.random.uniform(.1, 1, 100) ** 3 * 4,
y_name: np.random.normal(.053, .01, 100)})
df1after = pd.DataFrame({'Speed': np.random.uniform(.1, 1, 100) ** 3 * 4,
y_name: np.random.normal(.057, .01, 100)})
fig, ax = plt.subplots(figsize=(20, 10), dpi=100)
ax.scatter(x='Speed', y=y_name, data=df1before, color='r', marker='1', label='Before')
ax.scatter(x='Speed', y=y_name, data=df1after, color='b', marker='x', label='After')
ax.set_xlabel('Speed(Knot)')
ax.set_ylabel('Port ME Homemade Mass Flow Rate (kg/s)')
mean_before = df1before[y_name].mean()
ax.axhline(df1before[y_name].mean(), color='r', ls='--')
ax.text(1, mean_before, f'mean: {mean_before:.4f}\n',
ha='right', va='center', color='r', transform=ax.get_yaxis_transform())
mean_after = df1after[y_name].mean()
ax.axhline(df1after[y_name].mean(), color='b', ls='--')
ax.text(1, mean_after, f'mean: {mean_after:.4f}\n',
ha='right', va='center', color='b', transform=ax.get_yaxis_transform())
ax.legend()
plt.tight_layout()
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.