Home > database >  How do I connect categorical scatter points with a vertical line?
How do I connect categorical scatter points with a vertical line?

Time:11-28

I have data in dataframe about different assets - let's say A,B,C,D. What I would like to do is create a chart that looks something like this: enter image description here

These assets are at a maximum of price n (let's say in our case 3.5), the dotted line and dotted circle show the historic minimum. Furthermore, it is possible to also display a range using 2 dotted circles (i.e. full circle would mean that this is the current maximum, and the range between two dotted circles indicates the range of the price in a given timeframe). I have all the data in df, but I would like to know whether you know how to display this either in matplotlib or seaborn. The data:

df = pd.DataFrame({'A': [3.5,2,1.5], 'B': [3.5,1.7,1.7],'C': [3.5,0.7,0.7],'D': [3.5,1.1,1.1]})

CodePudding user response:

You can draw an open scatter plot using facecolor='none'. And setting facecolor=None will get the default with the face color equal to the main color.

With plt.vlines() you can draw vertical lines between the minima and the maxima.

from matplotlib import pyplot as plt
import pandas as pd

df = pd.DataFrame({'A': [3.5,2,1.5], 'B': [3.5,1.7,1.7], 'C': [3.5,0.7,0.7], 'D': [3.5,1.1,1.1]})
xvals = df.columns
colors = plt.cm.Set1.colors[:len(xvals)]
for ind, row in df.iterrows():
    plt.scatter(xvals, row, color=colors, s=200, facecolor=None if ind == 0 else 'none')
plt.vlines(xvals, df.min(), df.max(), color=colors, ls='--')

plt.tight_layout()
plt.show()

scatter plot connected with vertical lines

  • Related