chartify icon indicating copy to clipboard operation
chartify copied to clipboard

Coloured multi line chart with x-axis df.index?

Open brezn1 opened this issue 7 years ago • 2 comments

I guess its more a question than a feature request:

The data:

data1 = np.random.normal(0, 0.1, 1000)
data2 = np.random.normal(1, 0.4, 1000) + np.linspace(0, 1, 1000)
data3 = 2 + np.random.random(1000) * np.linspace(1, 5, 1000)
data4 = np.random.normal(3, 0.2, 1000) + 0.3 * np.sin(np.linspace(0, 20, 1000))

data = np.vstack([data1, data2, data3, data4]).transpose()

df = pd.DataFrame(data, columns=['data1', 'data2', 'data3', 'data4'])
df.head()
data1 data2 data3 data4
0 -0.216306 0.769149 2.614268 3.022906
1 0.091122 2.037440 2.135203 3.127771
2 0.076178 0.983550 2.278488 2.970982
3 -0.023629 1.859270 2.005969 2.986248
4 0.076633 1.453625 2.120465 2.805301

now, plotting in Pandas is done like that:

df.plot(title='Line plot')

image The unnamed dataframe-index is automatically used for the x-axis.

Question: Would it be possible to have a similar behaviour in chartify?

ch = chartify.Chart(blank_labels=True, x_axis_type='linear')
ch.plot.line(
   # Data must be sorted by x column
   data_frame=df,
   x_column= "index", # internally do a df.index.get_values() or just the fixed term "index"? 
   y_column="value",
   color_column="variable")

According to the tidy data paradigm, this solution works. But it seeems like a lot of extra miles to get the same result like in pandas:

melted_data = pd.melt(df.reset_index(), 
                      id_vars='index',  
                      value_vars=df.columns)

ch = chartify.Chart(blank_labels=True, x_axis_type='linear')
ch.plot.line(
    data_frame=melted_data,
    x_column= "index",   
    y_column="value",
    color_column="variable")

I have the feeling i won't be the last person to ask exactly this queston about grabbing the index-data. ;-)

brezn1 avatar Nov 28 '18 22:11 brezn1

Agree it would be helpful to have plotting methods straight from pivoted data for convenience.

In addition to #9 we could probably also solve for line, scatter, text, etc. Some of the functionality would have to be limited -- e.g. it would be hard to provide control over both scatter size and x-y coordinates from pivoted data.

cphalpert avatar Nov 29 '18 02:11 cphalpert

Thanks, sounds good.

I have seen this problem across other visualization libs, too. Some do it in the re-index/melt manner like chartify. Plotnine does allow it that way: x_column= "df.index"

brezn1 avatar Nov 29 '18 13:11 brezn1