Home > front end >  How to make dotted and bold lines in the same plot
How to make dotted and bold lines in the same plot

Time:09-23

I have a list and i want to plot the list in such a way that for certain range of x axis the lines are dotted while for other range it is solid. e.g.:

y=[11,22,33,44,55,66,77,88,99,100]
x=[1,2,3,4,5,6,7,8,9,10]

i did this:

if i range(4,8):
   plt.plot(x,y,marker='D')
else :
   plt.plot(x,y,'--')
plt.show()

but this doesnot work.

can someone help?

CodePudding user response:

Slice the data into 3 intervals

import matplotlib.pyplot as plt
import numpy as np

# Data for plotting

x = [1,2,3,4,5,6,7,8,9,10]
y = [11,22,33,44,55,66,77,88,99,100]
fig, ax = plt.subplots()

m, n = 4, 8

x1, x2, x3 = x[:m 1], x[m:n 1], x[n:]
y1, y2, y3 = y[:m 1], y[m:n 1], y[n:]

ax.plot(x1, y1, color='red', linestyle='solid', marker='D')
ax.plot(x2, y2, color='blue', linestyle='dashed')
ax.plot(x3, y3, color='red', linestyle='solid', marker='D')


plt.show()

CodePudding user response:

Here is a solution with the same colours for the whole line:

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10]
y = [11,22,33,44,55,66,77,88,99,100]
fig, ax = plt.subplots()

x1, y1 = x[:4], y[:4]
x2, y2 = x[3:8], y[3:8]
x3, y3 = x[7:], y[7:]

ax.plot(x1, y1, marker='D', color='b')
ax.plot(x2, y2, '--', color='b')
ax.plot(x3, y3, marker='D', color='b')

CodePudding user response:

Change line characteristics based on the value of x:

import numpy as np
from matplotlib import pyplot as plt
  • Make arrays of the lists;

     y = np.array([11,22,33,44,55,66,77,88,99,100])
     x = np.array([1,2,3,4,5,6,7,8,9,10])
    
  • make a boolean array based on your condition(s);

     dashed = np.logical_or(x<4,x>=8)
    
  • use the boolean array to filter the data when you plot.

     plt.plot(x[~dashed],y[~dashed],color='blue',marker='D')
     plt.plot(x[dashed],y[dashed],color='blue',ls='--')
    
  • Related