Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom plot linestyle in matplotlib
    text
    copied!<p>I'm trying to achieve graph using <code>matplotlib</code> with lines with whitespaces near points like in this one:</p> <p><a href="http://simplystatistics.org/wp-content/uploads/2013/01/Beijingair.png" rel="nofollow noreferrer">graph.png http://simplystatistics.org/wp-content/uploads/2013/01/Beijingair.png</a></p> <p>I know about <code>set_dashes</code> function, but it sets periodic dashes from start-point without control over end-point dash.</p> <p><strong>EDIT:</strong> 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 <code>pandas</code> and, strangely, works not exactly as I expected - I want equal offsets, but somehow they are clearly relative to the length.</p> <pre><code>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']&gt;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() </code></pre> <p><img src="https://i.stack.imgur.com/0sGCO.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload