Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:39:55+00:00 2026-06-17T20:39:55+00:00

I’m trying to achieve graph using matplotlib with lines with whitespaces near points like

  • 0

I’m trying to achieve graph using matplotlib with lines with whitespaces near points like in this one:

graph.png
(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()

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:39:56+00:00Added an answer on June 17, 2026 at 8:39 pm

    Ok, I’ve made a somewhat satisfactory solution. It is wordy and still a bit hackish, but it works! It provides a fixed display offset around each point, it stands against interactive stuff – zooming, panning etc – and maintains the same display offset whatever you do.

    It works by creating a custom matplotlib.transforms.Transform object for each line patch in a plot. It is certainly a slow solution, but plots of this kind are not intended to be used with hundreds or thousands of points, so I guess performance is not such a big deal.

    Ideally, all those patches are needed to be combined into one single “plot-line”, but it suits me as it is.

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    
    class MyTransform(mpl.transforms.Transform):
      input_dims = 2
      output_dims = 2
      def __init__(self, base_point, base_transform, offset, *kargs, **kwargs):
        self.base_point = base_point
        self.base_transform = base_transform
        self.offset = offset
        super(mpl.transforms.Transform, self).__init__(*kargs, **kwargs)
      def transform_non_affine(self, values):
        new_base_point = self.base_transform.transform(self.base_point)
        t = mpl.transforms.Affine2D().translate(-new_base_point[0], -new_base_point[1])
        values = t.transform(values)
        x = values[:, 0:1]
        y = values[:, 1:2]
        r = np.sqrt(x**2+y**2)
        new_r = r-self.offset
        new_r[new_r<0] = 0.0
        new_x = new_r/r*x
        new_y = new_r/r*y
        return t.inverted().transform(np.concatenate((new_x, new_y), axis=1))
    
    def my_plot(X,Y):
      ax = plt.gca()
      line, = ax.plot(X, Y, marker='o', linestyle='')
      color = line.get_color()
    
      size = X.size
      for i in range(1,size):
        mid_x = (X[i]+X[i-1])/2
        mid_y = (Y[i]+Y[i-1])/2
    
        # this transform takes data coords and returns display coords
        t = ax.transData
    
        # this transform takes display coords and 
        # returns them shifted by `offset' towards `base_point'
        my_t = MyTransform(base_point=(mid_x, mid_y), base_transform=t, offset=10)
    
        # resulting combination of transforms
        t_end = t + my_t
    
        line, = ax.plot(
          [X[i-1], X[i]],
          [Y[i-1], Y[i]],
          linestyle='-', color=color)
        line.set_transform(t_end)
    
    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()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.