I am using Holoviews for creating a visual dashboard and I want to create a Dynamic Tree Map within it. I tried finding documents, but I did not find any documentation of TreeMaps in the official Holoviews.
I then used Squarify library for plotting the TreeMaps but the Only Problem I am facing is that, I am not able to deploy these treemaps on server. When I start the server this Error message Pops up.
File "C:\Users\Nisarg.Bhatt\WinPython\python-3.6.5.amd64\lib\site-
packages\holoviews\plotting\util.py", line 236, in initialize_dynamic
dmaps = obj.traverse(lambda x: x, specs=[DynamicMap])
AttributeError: 'AxesSubplot' object has no attribute 'traverse'
CODE for TreeMap:
cmap = matplotlib.cm.Blues
mini=min(data["Quarter"])
maxi=max(data["Quarter"])
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
#colors = [cmap(norm(value)) for value in data]
data1=data[0:1]
labels = [(label) for label in zip(data.Quarter,data.Company)]
fig = plt.figure(figsize=(20, 10))
plots=squarify.plot(sizes=data["month"],label= labels, color=
["red","blue","green"],alpha=.8)
new_plot= renderer.app(plots)
When this is executed, The TreeMaps are created in the Jupyter NoteBook.
QUERY:
1. Is there some other way of plotting TreeMap using just holoviews like hv.Curve or hv.Bars
How to solve this server issue.
Thanks in Advance.
Regards,
Nisarg
Use a lower level library as bokeh (holoviews compatible) where you can plot rectangles as squarify does.
#plotting TreeMap using squarify.plot
data = [8,3,4,2]
df = pd.DataFrame(data, columns=['nb_people'])
label=df.groupby(pd.cut(df["nb_people"], np.array([0,2,5,np.inf]),include_lowest=True,labels=['Group A','Group B','Group C'])).groups
squarify.plot(sizes=df['nb_people'], label=label, alpha=1)
plt.show()
squarify
Related
I am trying to plot a 3d function in python using matplotlib. For some reason I get the error "Invalid syntax (pyflakes E)" in the second line of the code provided when trying to plot it. I got this part from another person, and this works for them. The packages from matplotlib I have installed are mplot3d, cm, and Subplot. Perhaps there is another package I need?
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(projection='3d')
https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html#:~:text=In%C2%A0%5B3%5D%3A-,fig%20%3D%20plt.figure()%0Aax%20%3D%20plt.axes(projection%3D%273d%27),-With%20this%20three
It seems that you need to use ax = plt.axes(projection = 3d)
I am trying to animate a multi series line graph using plotly. However after days of going through the documentation I still can't seem to find a solution.
Currently my code is as follows:
df = px.data.gapminder().query("continent=='Oceania' ")
fig = px.line(df, x="year" , y="lifeExp", color="country" , animation_frame="year", animation_group="lifeExp" , range_y=[68,84] , range_x=[1950,2010])
plot(fig)
This however generates and empty plot. Please help.
I am able to successfully generate a scatter plot and a bar graph using similar code.
For better understanding please view below link :
I have found an exact example of what I am looking for implemented in R.
https://plot.ly/r/cumulative-animations/#cumulative-lines-animation
For the empty plot, try changing the default renderer by adding this above your code:
import plotly.io as pio
pio.renderers.default = 'notebook'
There is some documentation on different renderers.
There are a number of helpful posts for using LineCollections in Matplotlib.
I have working code, but am having trouble figuring out how to set the transparency of the lines. For example, in Pandas it's as easy as doing:
df.plot(kind='line',alpha=.25)
However, I chose the LineCollection method because I want to plot a dataframe with >15k lines and the above example does not work.
I've tried adding ax.set_alpha(.25) in my code:
fig, ax = plt.subplots()
ax.set_xlim(np.min(may_days), np.max(may_days))
ax.set_ylim(np.min(may_segments.min()), np.max(may_segments.max()))
line_segments = LineCollection(may_segments,cmap='jet')
line_segments.set_array(may_days)
ax.add_collection(line_segments)
ax.set_alpha(.05)
ax.set_title('Daily May Data')
plt.show()
but there is no change.
Unfortunately I cannot provide a sample of the data with which I'm working; however, I've found the second example this Matplotlib gallery doc to be easy to copy.
You do it the same way you'd do it in pandas.
line_segments = LineCollection(may_segments, cmap='jet', alpha=0.05)
I am attempting to make a heat map with holoviews (currently using the bokeh backend). I have a data frame ('dep_df') with 3 columns: X, Y, type. X and Y are the dimension labels, and type is a categorical variables b/n 0 and n (where n is an integer). Here's my code:
dep_hm = hv.HeatMap(dep_df[["X", "Y", "type"]], label="DEP population")
TOOLS = ['hover']
colors = palettes.d3['Category20b'][5]
%%opts HeatMap [width=300, height=300, xaxis=None, yaxis=None, show_grid=True]
grid_style = {'grid_line_color': 'white', 'grid_line_width': 1.5}
dep_hm.options(cmap=ListedColormap(colors), gridstyle=grid_style, tools=TOOLS, invert_axes=True)
The plot looks correct in Jupiter notebook except that the ygrid lines don't show (only xgrid), and its showing all tools instead of just 'hover' as I specified. Even with the grid lines that do show, there's always a missing gridline exactly in the middle (have had that issue even in straight bokeh implementations of this heatmap.
Another issue is that I've tried saving the file to HTML using both Bokeh.io and renderer.save() and in both cases, all formatting options are not executed (like not showing the axes, inverting the axes, and not showing full toolbar options). it seems to just save the plot with default options.
Thanks for your help.
renderer.save() doesn't read the notebook magic i.e. %%opts HeatMap [width=300, height=300, xaxis=None, yaxis=None, show_grid=True]
You have to use your_variable.options(width=300, height=300, xaxis=None, yaxis=None, show_grid=True) to make it stick. See http://holoviews.org/user_guide/Customizing_Plots.html Simplified format
Not sure about your other issue though.
I'm trying to use the latex symbol \odot as a marker in a scatter plot but I also need latex style ticks, but for some reason these two are not playing well together. I can successfully use marker=$\\odot$ with usetex=False, like this, but when I set it equal to true (to get the tick font right), I get ! LaTeX Error: File 'type1cm.sty' not found. I've already gone through to make sure I have the sty file installed and in the correct directory and that I have all the dependencies installed (as suggested here). Plus, I can still have usetex=True and use any of the normal pyplot markers, just not anything involving math font, but can I can have \odot in the label for the legend. Ive also already tried appending the rc params with amsmath but still keep getting the type1cm error. I've also tried using the raw string literal to no avail.
So basically when usetex=True, I can use math symbols in the label for the legend, just not as the actual marker. Has anyone experienced this issue before?
My current work around involves just plotting a large unfilled circle and overplotting a small filled circle (basically simulating the odot). Then I run into an issue with the legend so I basically have to create a transparent legend showing the large unfilled circles and then plot the smaller filled circles behind it by hand like this which ends up wonky, but this has the axes tick font I need. This becomes very frustrating if I have to change axes limits though, because I have to repeat the process of figuring out where to plot the small filled circles all over again.
Does anyone know if there is a better work around than this? Would it be possible to use the overplotting scheme like I have been, but then create a custom proxy artist to display the \odot symbol (in the different colors/sizes) in the legend?
Mac OSX, matplotlib 1.4.2, python 2.7, matplotlib is using pdfTeX thru TeX Live 2017/Mac Ports 2017
Edit: Here is my code
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
f, ax1 = plt.subplots(1,1)
x = np.arange(20)
y = x
ax1.scatter(x, y, marker='$\\odot$', edgecolors='b', s=200, label = 'Test') #used with usetex=False
#ax1.scatter(x, y, marker='o', edgecolors='b', s=200, label = 'Test') #used with usetex=True
ax1.tick_params(labelsize=24)
leg = ax1.legend(scatterpoints=1, loc='lower right', borderaxespad=0., handletextpad=0.)#, fontsize=18) # borderpad=0.,)
I'm not sure how much I can help without seeing your code, but this worked for me:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
x1 = [1,2]; x2 = [1,2]
y1 = [1,1]; y2 = [2,2]
mpl.rc('text', usetex = True)
fig, ax = plt.subplots(1,1)
ax.scatter(x1,y1, label='A1', marker=r'$\odot$',s=150, c='b')
ax.scatter(x2,y2, label='A2', marker=r'$\odot$',s=50, c='b')
ax.set_xlim(0,3)
ax.set_ylim(0,3)
ax.legend()
fig.show()
If this doesn't help let me know!