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

Plot Holoviews.Image using Pandas dataframe #3686

Closed
HuangJunye opened this issue May 3, 2019 · 3 comments
Closed

Plot Holoviews.Image using Pandas dataframe #3686

HuangJunye opened this issue May 3, 2019 · 3 comments

Comments

@HuangJunye
Copy link

HuangJunye commented May 3, 2019

I posted this questions on StackOverflow as well: https://stackoverflow.com/questions/55962945/plot-holoviews-image-using-pandas-dataframe

I'm trying to make a 2D color plot of my low temperature transport measurement. The data were taken and saved in a series of .dat files. The data looks something like this:

B (T), T(K) , V (V), I (A)

0, 4.01, -10.0, 0.001

0, 4.02, -9.9, 0.001 ...

Each file was taken at a specific magnetic field. (0 to 2 Telsa with 0.2T step) in total 11 files. I imported all the files and concatenate them into a single pandas dataframe which looks like this:

B T V I
0 4.01 -10.0 0.001
0 4.02 -10.0 0.001
... ... ... ...
0.2 4.01 -10.0 0.001
0.2 4.02 -10.0 0.001
... ... ... ...
0.4 4.01 -10.0 0.001
0.4 4.02 -10.0 0.001
... ... ... ...

Now I want to make a 2D color plot with V and B as the x and y axis and I as z (color). How do I do that with Holoview.Image?

I know it can be done using using seaborn heatmap but I want to use holoview for interactive plot and other functionalities. It also works with Holoviews.HeatMap, but HeatMap has a few problems. HeatMap axes are categorical not numerical, which makes it difficult and non-intuitive to set xticks, xlim, etc.

Holoviews.Image is more suitable for my data, but it accepts only gridded data, not columnar data like pandas dataframe.

I guess, in a nutshell, my question is how to convert pandas dataframe to gridded data in order to plot in Holoviews.Image element.

@ahuang11
Copy link
Collaborator

ahuang11 commented May 3, 2019

One way~

import pandas as pd
import holoviews as hv
import hvplot.xarray

df = pd.DataFrame({
    'B': [0, 1, 2],
    'T': [2, 3, 4],
    'I': [5, 6, 7]
}).set_index(['B', 'T'])
df.to_xarray().hvplot.image('B', 'T', cmap='RdBu_r')

image

@HuangJunye
Copy link
Author

@ahuang11 It works perfectly! Thanks a lot, Andrew!

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants