hvplot heatmap ordering axis - holoviews

Question is, can I order the axis of a heatmap in hvplot?
I have the following code:
df['col_1'] = df.col_1.astype(int).astype('category')
df['col_2'] = df.col_1.astype(int)
heatmap = df.hvplot.heatmap('col_1', 'col_2', 'col_3',
reduce_function=np.median)
heatmap
But when I run it, the col_1 and col_2 are in random orders
If I do a Pandas groupby:
df.groupby(['col_1', 'col_2]).col_3.median().unstack()
I get the table I want with the correct sorting of col_1 and col_2 as axis of the heatmap. My previous code then passed this dataframe to seaborn.heatmap()
Is there a way to force the ordering in hvplot?
Ben

Related

Plotting a legend using legend(x,y ....) but x axis are dates in R

I'm trying to put a legend on a line graph using legend(x,y, legend=c("","").... etc. I've changed the date to numeric data and used that for x and it plots, so I know the rest of its right. but when x is a date I'm not sure what to use for x to get the legend to show on the graph.
thanks

Forecast plot with x axis labels as date

I have a dataset like revenue and date.
I used arima to plot the data.
ts_data = ts(dataset$Revenue,frequency = 7)
arima.ts = auto.arima(ts_data)
pred = forecast(arima.ts,h=30)
plot(pred,xaxt="n")
When I plot the data, it produces plot like below.
My expectations are below,
I need to display values in Million for predicted values like 13.1M.
I need to show x-axis as date instead of data points numbers.
I tried several links but couldn't crack it. Below are the experiments I made,
Tried with start date and end date in ts_data that also doesnt work.My start date is "2019-09-27" and end date is "2020-07-02"
tried wit axis_date in plot function that also doesnt work.
Please help me to crack the same.
Thanks a lot.
You can specify axis tick values and labels with axis()
plot(pred,xaxt="n", yaxt="n") # deactivate x and y axis
yl<-seq(-5000000,15000000,by=5000000) # position of y-axis ticks
axis(2, at=yl, label=paste(yl/1000000, "M")) # 2 = left axis
You can specify the desired position of y axis ticks with at and the labels to be associated with label. In order to obtain the values like 10 M I have used the function paste to join the numbers with the letter M.
You could use the same method also for x-axis, even tough more efficient methods probably exist. Not having the specific data available I have used a generic spacing and labels only to give you the idea. Once you have set the desired position you can generate the sequence of dates associated with it (to generate a sequence of dates see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq.Date)
axis(1, at=seq(1,40,by=1), label=seq(as.Date("2019-09-27"),as.Date("2020-07-02"),by="week")) # 1 = below axis
You can also change the format of the dates displayed with format() for example label=format(vector_of_date, "%Y-%b-%d") to have year-month(in letter)-day.

How to select a subplot of the timeVariation function in OpenAir

I want to produce a plot showing diurnal variation per weekday and month. The timeVariation function produces the desired plot, along with three other subplots. This makes the subplot at the top hard to read:
library(openair)
mary <- importAURN(site = "my1", year = 2000)
timeVariation(mary,
pollutant = 'no2',
type = 'month')
I would like to plot only the top subplot showing weekdays. I tried using plot(myOutput, subset = "day.hour") as described in the
OpenAir manual:
plot(timeVariation(mary,
pollutant = 'no2',
type = 'month'),
subset = 'day.hour')
But that produces this:
This plot may contain the correct data, but the replication of the labels makes it overcrowded and very confusing. Is there a way to extract just the plot I want, formatted as shown in the top image?

Plot step-wise decrease in R with Categorical value on X-axis

I want to use a step plot to illustrate a process of elimination. I have a dateframe containing the number of candidates remaining after each step; it looks like this:
Step Candidates Count
1 26587
2 1761
3 849
4 130
The Step column is a categorical variable and I need to represent with the names of the actual steps; I am using numbers because I have not been able to plot when the Step column contains text.
I was able to produce the following figure with the command
plot(df, type = "s")
The problem is the X axis: I need to either get rid of the decimals and add a legend to name each step or, preferably, figure out some way to put the names of the steps in the Step column and populate the axis automatically.
I also want to show the same graph as a log but when I use:
plot(log(df), type = "s")
R gives me log values for both columns. This wouldn't be a problem if I could figure out how to plot the data with Step as a categorical variable but I just cannot figure out how.
My instinct is that this is a fairly simple problem but I've been struggling for most of this morning.
plot(df, type = "s", xaxt='n', log="y")
axis(1, at=1:4, labels=paste("step", 1:4))
Use
xaxt to suppress x-axis ticks and labels
log="y" to get y-axis on log scale
axis to add in the x-axis with labels argument used at specified points on x-axis
You may also want to tweak the labels on the y-axis

switching the place of x and y axis data in r

I have a vector of data which consists of 20,000 numbers ranging between 0 and 1, i want to plot this data where x axis is the number values and y axis is their frequencies.
|
Freq|
|
|
|______________
values
but when i use plot(vector) in R, it shows frequency on x axis named as index and number values on y.
In the arguments used by plot() function i couldn't find anything helpful.
does anybody know how i could do this?
If you want a plot of frequencies, the best type of plot to make would be a barplot and the easiest way to make a barplot is just to pass a table to barplot(). For example
barplot(table(vector))
or if you just want a needle-style plot
plot(table(vector))
would also work.
If you want to trim outliers from the table, you could try
barplot( table( vector[vector<quantile(vector, .98)] ) )
here we drop samples that are above the 98% quantile.

Resources