TypeError: 'UndefinedType' object is not callable - altair

I'm getting a TypeError: 'UndefinedType' object is not callable when running the following Altair code.
import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x=alt.X('Horsepower').axis(tickMinStep=50),
y=alt.Y('Miles_per_Gallon').title('Miles per Gallon'),
color='Origin',
shape='Origin'
)

Make sure you use a recent Vega-Altair version. Method-based syntax for setting channel options is not available in altair<=4.2.2. Install a higher version.

Related

'int' object has no attribute 'iloc' Viewing data Python/Pandas error

I am trying to view the imported data through pandas, however when I run the script I am presented with the error "int' object has no attribute 'iloc". I am new here and this is my first post so apologies if I don't know the rules. My code is below.
import pandas as pd
from matplotlib import pyplot
from pandas import read_csv
filename = '/Users/rahulparmeshwar/Documents/Algo Bots/Data/Live Data/Tester.csv'
data = read_csv(filename)
pd.set_option('display.width',100)
pd.DataFrame.head(1)
print(pd)
I am new to Python too.
You want to call head() on your dataframe data, not on the constructor pd.DataFrame.
data = read_csv(filename)
pd.set_option('display.width',100)
print(data.head(1))

what attributes can I set in Matplotlib's function ax.set()

From the Matplotlib's official website I can get that the function 'ax.set()' can bset attributes.But I haven't known what attributes can I set actually.For instance:
from matplotlib import pyplot as plt
import numpy as np
import matplotlib
fig=plt.figure(
figsize=(15,6),
dpi=120,
)
#definition layout
layout=(2,4)
#create
axes=fig.subplots(*layout)
ax_plot_function=axes[0][0]
ax_scatter=axes[0][1]
#plot the first axes
props = {
'title': 'function_example$f(x)=cos(x)$',
'xlabel': 'X',
'ylabel':'Y',
'xlim':(-(2*np.pi),2*np.pi),
'ylim':(-(np.pi),np.pi),
'grid':True,
}
ax_plot_function.set(**props)
x=np.arange(-(2*np.pi),2*np.pi,0.01)
y=np.cos(x)
ax_plot_function.plot(x,y)
plt.show()
in Line29 happens an error that says
AttributeError: 'AxesSubplot' object has no property 'grid'
So I know that the function 'set()' can not have the attribute 'grid' in Line27,But how can I fix it and What attributes can I set exactly by using the function 'ax.set()'.
Could I get a list of all attributes that can set by the function 'set()'? Or Please tell me where I can find the list.
ax.set() can set the properties defined in class matplotlib.axes.Axes.
There is also set_xxx() api where xxx is also the property in class matplotlib.axes.Axes.
The idea is simple, image you create a Python class and some properties. The related getter and setter method should be defined. Also, the constructor method can take initial values of those properties.
If you want to turn on the grid, use ax.grid(True).

vtkOBJReader object has no attribute SetFileNameMTL (.mtl) for Object (.obj) files

The code below complains that vtk.vtkOBJReader() object has no method SetFileNameMTL().
In the documentation it appears to exist vtkOBJImporter.SetFileNameMTL (Maybe the python wrapper for this is missing?).
How can the material (.mtl) for the object be set when parsing the (.obj) in vtk and made visible in k3d?
import k3d
import vtk
import ipywidgets as widgets
reader = vtk.vtkOBJReader()
reader.SetFileName('sample_obj/Model.obj')
reader.SetFileNameMTL('sample_obj/Model.mtl') #Attribute not found
reader.Update()
plot = k3d.plot()
poly_data = reader.GetOutput()
plot += k3d.vtk_poly_data(poly_data)
plot.display()
You are using vtkOBJReader, not vtkOBJImporter. Those are two different classes. vtkOBJReader is the older class, I think, and only reads in the geometry file. To load the material info, you need to use vtkOBJImporter.

How to display/save a Layered graph in altair by running code from Python console

After creating three different charts with altair graph API and then merging them as per altair documentation.
(underlay+base+overlay).save("layeredChart.html")
An html file is generated with name layeredChart.html
On opening the html file error comes:
JavaScript Error: Duplicate signal name: "selector002_tuple"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
What can be the reason for error in html file generation with altair though works fine with jupyter notebook??
Code:
import altair as alt
#altair plot
alt.data_transformers.disable_max_rows()
#Selection tool
selection = alt.selection_single(fields = ['Country/Region'])
#Underlay
base = alt.Chart(de_long).mark_line(strokeWidth=4,opacity=0.7).encode(
x = alt.X('Day'),
y = alt.Y('De',scale=alt.Scale(type='log')),
color = alt.Color('Country/Region',legend=None)
).properties(
width=800,
height=650
).interactive()
print(alt.renderers.names())
#Chart
chart1 = base.encode(
color=alt.condition(selection,'Country/Region:N',alt.value('lightgray'))).add_selection(selection)
#Overlay
overlay = base.encode(
color = 'Country/Region',
opacity = alt.value(0.5),
tooltip = ['Country/Region:N','Name:N']
).transform_filter(selection)
finalChart = (base+chart1+overlay)
finalChart.save("final.html")
This error generally means that you've called add_selection() with the same selection on multiple layers, which is not supported by the Vega-Lite renderer.
Here is a Minimal Reproducible Example of this error:
import altair as alt
import pandas as pd
df = pd.DataFrame({'x': range(10)})
selection = alt.selection_single()
base = alt.Chart(df).encode(x='x').add_selection(selection)
base.mark_line() + base.mark_point()
Javascript Error: Duplicate signal name: "selector001_tuple"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.
The way to fix it is to add the selection to only one of the layers; for example:
base = alt.Chart(df).encode(x='x')
base.mark_line() + base.mark_point().add_selection(selection)

TypeError: 'numpy.ndarray' object is not callable when import a function

Hi I am getting the following error.
TypeError: 'numpy.ndarray' object is not callable
I wrote a function module by myself,like this:
from numpy import *
import operator
def creatDataset() :
group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
labels = ['A','A','B','B']
return group,labels
then,I want to use this function in Microsoft's command window ,I've written some code, as follows:
import KNN
group,labels=KNN.creatDataset()
group()
when I input the code "group()",the error will appear.It's the first time that i describe the question and ask for help, maybe the description is not clear ,,please forgive me.
Since "group" is a numpy.array, you cannot call it like a function.
So "group()" will not work.
I assume, you want to see it's values, so you would have to use something like
"print(group)".

Resources