X Tutup
The Wayback Machine - https://web.archive.org/web/20201027080531/https://github.com/spotify/chartify/issues/45
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
brezn1 opened this issue Nov 28, 2018 · 2 comments
Open

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

brezn1 opened this issue Nov 28, 2018 · 2 comments

Comments

@brezn1
Copy link

@brezn1 brezn1 commented Nov 28, 2018

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. ;-)

@cphalpert
Copy link
Collaborator

@cphalpert cphalpert commented Nov 29, 2018

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.

@brezn1
Copy link
Author

@brezn1 brezn1 commented Nov 29, 2018

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.
X Tutup