Home > Back-end >  Create a line plot connecting three points each having a different marker
Create a line plot connecting three points each having a different marker

Time:08-16

Suppose I have three pandas series condition1, condition2, condition3 each containing over 23,000 float numbers. I want to make a plot with three connected points. Each should be a median of these three datasets and have a different marker.

The line and points should have a similar layout like the example figure (except with three points and only one line). example figure

This is what I tried:

medians = [np.median(condition1), np.median(condition2), np.median(condition3)]

plt.plot(medians[0], marker='o')
plt.plot(medians[1], marker='*')
plt.plot(medians[2], marker='D')
plt.show()

and this:

medians_df = pd.DataFrame({'cond1': [np.median(condition1)], 'cond2': [np.median(condition2)], 'cond3': [np.median(condition3)]})

for count, value in enumerate(medians_df):
    plt.plot(raw_medians[value], marker=markers[count])
plt.legend()
plt.show()

However, those plots only give three points that are aligned vertically. Also, it is not clear to me how I could add error bars.

The following piece of code gives me points that are connected with a line but there are no markers for each point.

plt.plot(medians)
plt.show()

Any help will be appreciated!

CodePudding user response:

If I understand you correctly, you can use matplotlib errorbars. I have taken the liberty of assuming your medians and errors, you of course can obtain it from your data.

import matplotlib.pyplot as plt

x = [0, 1, 2]
medians = [1, 5, 3.5]
error_bar_length = [0.5, 1, 4]
markers = ['s', 'o', 'D']

plt.plot(x, medians, color='black')  # Plot the line through the medians

# Plot the errorbars with different markers
for ii in range(3):
    plt.errorbar(x=x[ii], y=medians[ii], yerr=error_bar_length[ii], marker=markers[ii], color='black')
plt.show()
  • Related