Home > Net >  How to change the color of one datapoint of line graph
How to change the color of one datapoint of line graph

Time:11-26

I am trying to plot the following graph below for some datapoints missing in the graph. For the second bar the last data point, doesn't have any datapoint. I need this missing data point to be represented in red and doesnt want the line to go at 0. Any help is highly appreciated

plt.figure(figsize=(3,2))
no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

point_1 = [86.317,86.317, 86.3175, 86.317, 86.317, 86.317,86.317, 86.317,86.317, 86.317]
point_1 = [88.404, 88.404,88.404, 88.404, 88.404, 88.404, 88.404,88.404, 88.404, 88.404]


point_2 = [76.3671,85.0843,80.4446,83.8689,83.868,79.4610,83.6540,78.4579,82.666,0]
point_2 = [72.501,  81.245,76.507,80.989,81.191,77.570,81.817,78.354,82.230,0]

plt.plot(no, point_1, color='orange', marker='^', linestyle='dashed',label='Measured 1 ')
plt.plot(no, point_1, color='orange', marker='*', linestyle='dashed',label='Predicted 1')


plt.plot(no, point_2, color='grey', marker='^', linestyle='dashed',label='Measured 2')
plt.plot(no, point_2, color='grey', marker='*',linestyle='dashed',label='Predicted 2')

plt.ylim(0, 100)

plt.xticks(no,fontsize='8')

#str_x=[l for l in no if not l in measured_3]
#for s_x in str_x:#
#    plt.text(s_x,62,'*', color='r')

#plt.legend(loc='upper right',prop={'size': 3.5})

plt.xlabel('no.')
plt.ylabel('Ms')

graph

CodePudding user response:

Using a red line and a red point as you indicated is bad practice. I'd be very clear and use a red question mark.

All you'll have to do is

  • Replace the zero with numpy.nan so it gets ignored.
  • Put a text field into your graph. You can check in the docs how to make the text appear as you like.
import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(3,2))
no = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

point_1 = [86.317,86.317, 86.3175, 86.317, 86.317, 86.317,86.317, 86.317,86.317, 86.317]
point_1 = [88.404, 88.404,88.404, 88.404, 88.404, 88.404, 88.404,88.404, 88.404, 88.404]


point_2 = [76.3671,85.0843,80.4446,83.8689,83.868,79.4610,83.6540,78.4579,82.666,0]
point_2 = [72.501,  81.245,76.507,80.989,81.191,77.570,81.817,78.354,82.230,np.nan]

plt.plot(no, point_1, color='orange', marker='^', linestyle='dashed',label='Measured 1 ')
plt.plot(no, point_1, color='orange', marker='*', linestyle='dashed',label='Predicted 1')


plt.plot(no, point_2, color='grey', marker='^', linestyle='dashed',label='Measured 2')
plt.plot(no, point_2, color='grey', marker='*',linestyle='dashed',label='Predicted 2')

plt.ylim(0, 100)

plt.xticks(no,fontsize='8')

#str_x=[l for l in no if not l in measured_3]
#for s_x in str_x:#
#    plt.text(s_x,62,'*', color='r')

#plt.legend(loc='upper right',prop={'size': 3.5})

plt.xlabel('no.')
plt.ylabel('Ms')

plt.text(10, 82.230, "?", color="red", ha="center", va="center")
  • Related