There’s probably some hack, but let’s be honest: It would be nothing more than a dirty hack and could introduce a lot of confusion. We can display the image with matplotlib but have no information of the colormap. Ugh! How? All we have now is the histogram and a rasterized image. Here we set it to be logarithmic, but typically I’d recommend leaving it at the default which uses a more sophisticated method.Īnd now let’s just add a color bar to the plot. You can decide how you want to normalize the data (using the how parameter). Image by author.ĭots can no longer partially overlap, and since you’re creating a histogram the colormap will handle your previous opacity problem. There are the additional packages we need: pip install datashader, colorcet, pandasĪnd that is the code: import datashader as ds import pandas as pd import colorcet as cc import matplotlib.pyplot as plt df = pd.DataFrame(data=X, columns=) # create a DF from array cvs = ds.Canvas(plot_width=500, plot_height=500) # auto range or provide the `bounds` argument agg = cvs.points(df, 'x', 'y') # this is the histogram img = ds.tf.set_background(ds.tf.shade(agg, how="log", cmap=cc.fire), "black").to_pil() # create a rasterized image plt.imshow(img) plt.axis('off') plt.show() Note that datashader only accepts DataFrame as input (be it pandas, dask or others) and your data must be stored as float32. I really like fire from the colorcet library. So all that’s left is to apply the colormap. The histogram you’ve created is already the same shape as your image. In this case, a 2D-histogram with equal-width bins. If this process sounds familiar to you, then that’s because that’s how you create a histogram. And then it just checks which bin each sample occupies. Instead, datashader will divide your 2D-space into width horizontal and height vertical bins. Typically, each point will occupy multiple pixels. The main improvement comes from the rasterization process: matplotlib will create a circle for every data point and then, when you’re displaying your data, it will have to figure out which pixels on your canvas each point occupies. datashader - the solution?ĭatashaderis a great library to visualize larger datasets. Top, left: alpha=0.1 Top, right: s=0.1 alpha=0.1 Bottom left: s=1 alpha=0.1 Bottom right: s=1.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |