Date is represented after a gap of 4-5 days on the axis. I want it to be represented in all bars - jupyter-notebook

df = px.data.tips() plotd_2022_dec = px.bar(days_2022_dec, x="Students", y="Date", orientation='h') plotd_2022_dec.update_layout(bargap=0.23,height=724) plotd_2022_dec.show()
How can it be done so that the date is represented on each bars of the plot
see image of plot
what can be done to resolve the isssue

Related

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.

Plotly histogram is missing data

I'm trying to create a histogram which involves a lot of repeated values in one of the cases. One of the data points is not being represented in the graph. Here is the smallest, simplest subset I could find that still reproduced my issue.
cleanVar <- c(rep(1,9),1.25,1.5)
plot_ly(data.table(cleanVar),
x = ~cleanVar,
type = "histogram")
The above graph shows only two bars. One centered at 1 of height 9, and one centered at 1.2 of height 1.
Also strangely, the hover-over shows "1" for the first bar, despite it covering the range [.9,1.1], and it shows "1.25" for the second bar, despite it covering the range [1.1,1.3].
If we change the 1 to only be repeated 8 times cleanVar <- c(rep(1,8),1.25,1.5), so that there are 10 total values in the histogram, it works better, but still, the three bins it creates are .25 wide according to the hover-over, yet they are only .2 wide on the graph itself.
What is plotly doing? How can I properly show 3 bins of height 9,1,1 and width .25? binning options in layout() aren't working.
By default plotly uses the following procedure to define the bins:
start:
Sets the starting value for the x axis bins. Defaults to the minimum data value, shifted down if necessary to make nice round values and to remove ambiguous bin edges. For example, if most of the data is integers we shift the bin edges 0.5 down, so a size of 5 would have a default start of -0.5, so it is clear that 0-4 are in the first bin, 5-9 in the second, but continuous data gets a start of 0 and bins [0,5), [5,10) etc. Dates behave similarly, and start should be a date string. For category data, start is based on the category serial numbers, and defaults to -0.5. If multiple non-overlaying histograms share a subplot, the first explicit start is used exactly and all others are shifted down (if necessary) to differ from that one by an integer number of bins.
end:
Sets the end value for the x axis bins. The last bin may not end
exactly at this value, we increment the bin edge by size from
start until we reach or exceed end. Defaults to the maximum data
value. Like start, for dates use a date string, and for category
data end is based on the category serial numbers.
You can find this information via:
library(listviewer)
schema(jsonedit = interactive())
Navigate as follows: object ► traces ► histogram ► attributes ► xbins ► start
To avoid the default behaviour just make your x variable a factor:
library(plotly)
library(data.table)
cleanVar <- c(rep(1, 9), 1.25, 1.5)
plot_ly(data.table(cleanVar),
x = ~factor(cleanVar),
type = "histogram")
Result:

gnuplot frequency chart timestamp

I have a list of timestamps like shown at the end, and I am trying to make a histogram/frequency chart where the X-axis is the time and the y axis a count of the frequency. I tried doing something like this:
set term dumb
set xdata
set timefmt '%Y-%m-%dT%H:%M:%SZ'
set xrange ["16/04/2018":"14/04/2018"]
plot 'dat'
and in the data file
2018-04-16T14:54:38Z
2018-04-16T13:50:07Z
2018-04-16T12:30:45Z
2018-04-16T11:45:16Z
2018-04-16T11:44:11Z
2018-04-16T10:19:19Z
2018-04-16T10:13:25Z
2018-04-16T00:17:33Z
2018-04-15T23:10:15Z
2018-04-14T22:27:34Z
2018-04-14T23:11:29Z
2018-04-14T22:45:08Z
Result
As you can see the x axis does not represent the time.
EDIT:
With Christoph's fixed code, the result is a plot of the timestamp and row the number.
To better explain the problem I have:
I want to plot the time on the x-axis and I want to plot the number of occurrences of the timestamp on the y-axis and where the timestamps are binned to some value in hours and possibly instead of just drawing bars it would interpolate lines between the peaks of the bars.
I asked for you actual question, because the code you posted doesn't create any plot. Let's see:
If I execute only the code you posted, I get the error all points y value undefined!.
In plot time data on the x axis, you need set xdata time.
Having set that you would get as next error Need full using spec for x time data. Indeed, you must have a using when handling time data, something like
plot 'dat' using 1:0
Then, you would again get the error all points y value undefined!. This time, because the time strings given in set xrange must match those specified with set timefmt.
So, a working version of your code could be:
reset
set xdata time
set timefmt '%Y-%m-%dT%H:%M:%SZ'
set xrange ["2018-04-16":"2018-04-14"]
plot 'dat' using 1:0
This plots time on the xaxis, and the row number on the y-axis.

Excel scatter plot x axis displays only sequential numbers but not real data selected for x axis

I have an excel scatter plot with 5 different data series on single chart. First 4 series are working well. When I want to add a new series with similar x-axis data (0.0, 0.4, 0.9 .. ) the plot is displayed with x-axis values as 1,2,3 but not as the data specified.
Changing the chart types did not help. Not sure how can I get the x-axis as data but not as sequential numbers. Any help is appreciated. Thanks.
Added the screenshot of chart and its xaxis data. The values are in number format only just as data for other series. Everytime I am adding a new series on to this, its starting with one number later.... (1,2,3...) next series x axis at (2,3,4....) but not with real x values as selected.
Solved it my slef... The problem is X-axis range is for 18 cells and all the cells had formula with IF condition... When I removed the IF condition, x-axis worked well as numbers
The IF condition I used was "=IF(A10<>"",B10=A10-A4,""), for some reason excel chart considered this as some text and populated the x axis as 1,2,3 but not as the values specified.

R: setting label spacing and position on a bar plot

I want to present percentages over a 24h period in 15 min intervals as a bar plot.
When I use barplot(), the labels for those timepoints are more or less randomly chosen by R (depending on how I format the window. I know it's not random, but it's not what I want either). I would rather have them evenly spaced at 1 h intervals (that is every 4th bar).
I have searched extensively on this and know i can add labels later with axis() but I have not found a way to set which bars are labeled and which are left blank.
So here is an example. Sorry for the long lines:
x<-sample(1:100,96)
Labels<-c("09","09:15","09:30","09:45","10:00","10:15","10:30","10:45","11","11:15","11:30","11:45","12","12:15","12:30","12:45","13","13:15","13:30","13:45","14","14:15","14:30","14:45","15","15:15","15:30","15:45","16","16:15","16:30","16:45","17","17:15","17:30","17:45","18","18:15","18:30","18:45","19","19:15","19:30","19:45","20","20:15","20:30","20:45","21","21:15","21:30","21:45","22","22:15","22:30","22:45","23","23:15","23:30","23:45","00","00:15","00:30","00:45","01","01:15","01:30","01:45","02","02:15","02:30","02:45","03","03:15","03:30","03:45","04","04:15","04:30","04:45","05","05:15","05:30","05:45","06","06:15","06:30","06:45","07","07:15","07:30","07:45","08","08:15","08:30","08:45")
names(x)<-Labels
barplot(x)
I do not think you can force R to show every label if it does not have enough space. But at least if you want to add the labels every 1h, the following code should work :
x<-sample(1:100,96)
Labels<-c("09","09:15","09:30","09:45","10","10:15","10:30","10:45","11","11:15","11:30","11:45","12","12:15","12:30","12:45","13","13:15","13:30","13:45","14","14:15","14:30","14:45","15","15:15","15:30","15:45","16","16:15","16:30","16:45","17","17:15","17:30","17:45","18","18:15","18:30","18:45","19","19:15","19:30","19:45","20","20:15","20:30","20:45","21","21:15","21:30","21:45","22","22:15","22:30","22:45","23","23:15","23:30","23:45","00","00:15","00:30","00:45","01","01:15","01:30","01:45","02","02:15","02:30","02:45","03","03:15","03:30","03:45","04","04:15","04:30","04:45","05","05:15","05:30","05:45","06","06:15","06:30","06:45","07","07:15","07:30","07:45","08","08:15","08:30","08:45")
b=barplot(x,axes = F)
axis(2)
axis(1,at=c(b[seq(1,length(Labels),4)],b[length(b)]+diff(b)[1]),labels = c(Labels[seq(1,length(Labels),4)],"09"))

Resources