I'm trying to achieve graph using matplotlib
with lines with whitespaces near points like in this one:
(source: simplystatistics.org)
I know about set_dashes
function, but it sets periodic dashes from start-point without control over end-point dash.
EDIT: I made a workaround, but the resulting plot is just a bunch of usual lines, it is not a single object. Also it uses another library pandas
and, strangely, works not exactly as I expected - I want equal offsets, but somehow they are clearly relative to the length.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
def my_plot(X,Y):
df = pd.DataFrame({
'x': X,
'y': Y,
})
roffset = 0.1
df['x_diff'] = df['x'].diff()
df['y_diff'] = df['y'].diff()
df['length'] = np.sqrt(df['x_diff']**2 + df['y_diff']**2)
aoffset = df['length'].mean()*roffset
# this is to drop values with negative magnitude
df['length_'] = df['length'][df['length']>2*aoffset]-2*aoffset
df['x_start'] = df['x'] -aoffset*(df['x_diff']/df['length'])
df['x_end'] = df['x']-df['x_diff']+aoffset*(df['x_diff']/df['length'])
df['y_start'] = df['y'] -aoffset*(df['y_diff']/df['length'])
df['y_end'] = df['y']-df['y_diff']+aoffset*(df['y_diff']/df['length'])
ax = plt.gca()
d = {}
idf = df.dropna().index
for i in idf:
line, = ax.plot(
[df['x_start'][i], df['x_end'][i]],
[df['y_start'][i], df['y_end'][i]],
linestyle='-', **d)
d['color'] = line.get_color()
ax.plot(df['x'], df['y'], marker='o', linestyle='', **d)
fig = plt.figure(figsize=(8,6))
axes = plt.subplot(111)
X = np.linspace(0,2*np.pi, 8)
Y = np.sin(X)
my_plot(X,Y)
plt.show()