Why does plot use floats on X-axis when an Int16[] was supplied?


#1

I am trying to find my feet in Julia and Plotting.

When I plot df (see below) the xticks shows values like

2005.0 2007.5 2010.0 2012.5 2015.0

while the range is of the type Int16[].

I could, after several hours of reading, not find a way how to change that to

2005  2007  2009 2011 2013 2015

By the way is there a better way to convert :pubyear to an array of integers than I have done?

function pyint(x::Array{Any,1})
    # convert array to Int16
    y = Int16[]
    for i in x
        push!(y, convert(Int16, i))
    end
    y
end

function plot_world_share(df)
    x = pyint(df[:pubyear])
    @df df plot(x, [:Biological_Sciences :Chemistry :Geological_Sciences  :Computer_Science :Mathematics :Physics :Statistics         ])
    end

df = get_world_share()  #Not shown here
df[:pubyear]
11-element Array{Any,1}: # [2005:2015]

Regards
Johann


#2

I don’t know about the plotting but you can do Int16.(x) to convert your array.


#3

Plots converts everything to Float64’s internally. I can see how that can be annoying at times.
With respect to setting the x ticks you can just

@df df plot(x, [:Biological_Sciences :Chemistry :Geological_Sciences  :Computer_Science :Mathematics :Physics :Statistics], xticks = 2005:2:2015)

The best reference for plotting if you want to use the Plots ecosystem (there are several alternatives) is the tables on this page: https://juliaplots.github.io/attributes/