Plotting only certain dates in an array - date

I am new to python programming, and have been trying to put together a plot of numbers against dates. The only problem is, when I go to plot it individually seems to show every single date in my range of dates. Consequently the x axis is completely illegible. Is there some way to keep all my data points, but have the x axis only display dates by the month, or something similar?
The code, with some sample data, is as below:
import datetime as dt
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
dates=['1/08/2015', '2/08/2015', '1/09/2015', '2/09/2015', '3/09/2015','4/09/2015', '5/09/2015', '1/10/2015', '2/11/2015', '3/11/2015', '4/11/2015', '5/11/2015', '1/12/2015', '2/12/2015', '1/01/2016', '2/01/2016', '3/01/2016', '1/02/2016', '2/02/2016', '3/02/2016', '4/02/2016', '1/03/2016', '6/03/2016', '1/04/2016', '2/05/2016', '1/06/2016', '1/07/2016', '2/07/2016', '3/07/2016', '4/07/2016', '5/07/2016', '1/08/2016', '2/08/2016', '3/08/2016']
converteddates= [dt.datetime.strptime(d, '%d/%m/%Y').date() for d in dates]
data=range(len(converteddates))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(converteddates,data)
plt.gcf().autofmt_xdate()
plt.show()

You should be using MonthLocator (DayLocator's job is to put ticks at everyday):
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.plot(converteddates,data)
plt.gcf().autofmt_xdate()
But a better approach might be:
fig, ax = plt.subplots()
ax.plot_date(converteddates, data)
and if it still doesn't like your dates, then set the ticks and labels underneath:
ax.set_xticks(converteddates)
strdates = [d.strftime("%d/%m/%Y") for d in converteddates]
ax.set_xticklabels(strdates, rotation="45")

Related

Modyfing python code and running PCA in Tableau

I am a beginner, my first time to use Tableau. I want to perfrorm PCA from Python code in Tableau Dekstop. I got main ideas behind that process, TabPy is installed.
My dataset is really big, having around 1000 + columns.
I took a look on modyfing python code (my python code at the end) to be able to run in tableau.
My question is, in my case how can specify _arg1,_arg2,_arg3,... because I used dataset.drop('Class', 1) to define x, and dataset['Class'] to define y?
Thank you in advance.
# importing or loading the dataset
dataset = pd.read_excel('NL_undivided.xlsx')
# distributing the dataset into two components X and Y
X = dataset.drop('Class', 1)
Y = dataset['Class']
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X)
scaled_data = scaler.transform(X)
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(scaled_data)
x_pca = pca.transform(scaled_data)
plt.figure(figsize=(20,10))
fig, ax = plt.subplots(figsize=(20, 10))
scatter = ax.scatter(x_pca[:,0],x_pca[:,1],c=Y,cmap='rainbow',)
# produce a legend with the unique colors from the scatter
legend1 = ax.legend(*scatter.legend_elements(),
loc="best", title="Cohorts")
ax.add_artist(legend1)
plt.figure(figsize=(15,8))

Plot range for line of fit using date on the x-axis in gnuplot

I am trying to plot the fit function (col 3 versus col 7) for my dataset given below
28-08-1991 20-12-1992 24-04-1992 -263347200 -221875200 -242611200 0.060859
20-12-1992 02-09-1993 27-04-1993 -221875200 -199756800 -210816000 0.064681
10-09-1996 13-09-1997 13-03-1997 -104371200 -72576000 -88473600 0.095728 ......
I would like my script to plot the fit only for certain range of dates, say between 1995 to 2003. I am unable to get the range function right since the gnuplot handle the dates past the year 2000.
Please find my code below.
set xdata time
set timefmt "%d-%m-%Y"
set xrange ["01-01-1991":"01-01-2015"]
set yrange [0.0:0.4]
offset = 24*60*60
f(x) = 1.e-7*m*(x-offset)+c
fit f(x) "file" using 3:7 via m,c
set fit logfile 'file.log'
plot '(x < "24-02-1993" ) ? f(x) : 1/0' title "Best fit" with lines ls 5
The variable x in plot will contain the time in the form of seconds since 1/1/1970. So in order to compare it with a date, you need to convert that date to the same format with strptime.
If you additionally remove the ' around the function to plot in your MWE, you get:
plot (x<strptime("%d-%m-%Y","24-02-1993"))?f(x):1/0 title "Best fit" with lines ls 5
Note that you've fitted f(x) over the whole dataset, not over dates before 24-02-1993, it's only the plot which is restricted to these dates.

How to import dates into MATLAB

I am using MATLAB R2015b. I am trying to import a excel file full of dates using xlsread('filename.xls'). The dates looks like the followings:
02/01/1996
03/01/1996
04/01/1996
05/01/1996
08/01/1996
then I want use datevec to separate the day month and year. for date = '02/01/1996'(January 2nd 1996), datevec gives Y= 1996, M = 2, D= 1,H=0 MN=0 S=0. For date '29/12/2000' (Dec 29 2000), datevec gives Y = 35, M=5 D =23 H=0 MN=0 S=0.
I tried to change the date format in excel, but it still does not work. Can anyone let me know how can I fix this please?
DateVector = datevec(DateString,formatIn)
As per the documentation. Set your formatIn correct:
DateString = {'16/09/2007';'14/05/1996';'29/11/2010'};
formatIn = 'dd/mm/yyyy';
datevec(DateString,formatIn)
Since MATLAB is an American program all their defaults are American (hence you're not able to call your colourbar as you want to). You just have to select a different date format.

Chaco - Getting multiple data series to use the same axes and maps

I am trying to plot several collections of data on a single plot.
Each dataset can be represented as an x-series (index) and several y-series (values). The ranges of x and y data series may be different in each data set. I want to have several of these data sets display on one plot. However, when I simply add the second plot object to the first (see below) it makes a second axis for it that is nested inside the plot.
I want both plots to share the same axis and for the axis bounds to be updated to fit all the data. What is the best way to achieve this? I am struggling to find topics on this in the documentation.
Thanks for your help. The code below highlights my problem.
# Major library imports
from numpy import linspace
from scipy.special import jn
from chaco.example_support import COLOR_PALETTE
# Enthought library imports
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, Group, View
# Chaco imports
from chaco.api import ArrayPlotData, Plot
from chaco.tools.api import BroadcasterTool, PanTool, ZoomTool
from chaco.api import create_line_plot, add_default_axes
def _create_plot_component():
# Create some x-y data series to plot
x = linspace(-2.0, 10.0, 100)
x2 =linspace(-5.0, 10.0, 100)
pd = ArrayPlotData(index = x)
for i in range(5):
pd.set_data("y" + str(i), jn(i,x))
#slightly different plot data
pd2 = ArrayPlotData(index = x2)
for i in range(5):
pd2.set_data("y" + str(i), 2*jn(i,x2))
# Create some line plots of some of the data
plot1 = Plot(pd)
plot1.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="red")
# Tweak some of the plot properties
plot1.title = "My First Line Plot"
plot1.padding = 50
plot1.padding_top = 75
plot1.legend.visible = True
plot2 = Plot(pd2)
plot2.plot(("index", "y0", "y1"), name="j_n, n<3", color="green")
plot1.add(plot2)
# Attach some tools to the plot
broadcaster = BroadcasterTool()
broadcaster.tools.append(PanTool(plot1))
broadcaster.tools.append(PanTool(plot2))
for c in (plot1, plot2):
zoom = ZoomTool(component=c, tool_mode="box", always_on=False)
broadcaster.tools.append(zoom)
plot1.tools.append(broadcaster)
return plot1
# Attributes to use for the plot view.
size=(900,500)
title="Multi-Y plot"
# # Demo class that is used by the demo.py application.
#===============================================================================
class Demo(HasTraits):
plot = Instance(Component)
traits_view = View(
Group(
Item('plot', editor=ComponentEditor(size=size),
show_label=False),
orientation = "vertical"),
resizable=True, title=title,
width=size[0], height=size[1]
)
def _plot_default(self):
return _create_plot_component()
demo = Demo()
if __name__ == "__main__":
demo.configure_traits()
One of the warts in Chaco (and indeed many plotting libraries) is the overloading of terms---especially the word "plot".
You're creating two different (capital-"P") Plots, but (I believe) you really only want one. Plot is the container that holds all of your individual line ... umm ... plots. The Plot.plot method returns a list of LinePlot instances (this "plot" is also called a "renderer" sometimes). That renderer is what you want to add to your (capital-"P") Plot container. The plot method actually creates the LinePlot instance and adds it to the Plot container for you. (Yup, that's three different uses of "plot": The container, the renderer, and the method on the container that adds/returns the renderer.)
Here's a simpler version of _create_plot_component that does roughly what you want. Note that only a single (capital-"P") Plot container is created.
def _create_plot_component():
# Create some x-y data series to plot
x = linspace(-2.0, 10.0, 100)
x2 =linspace(-5.0, 10.0, 100)
pd = ArrayPlotData(x=x, x2=x2)
for i in range(3):
pd.set_data("y" + str(i), jn(i,x))
# slightly different plot data
for i in range(3, 5):
pd.set_data("y" + str(i), 2*jn(i,x2))
# Create some line plots of some of the data
canvas = Plot(pd)
canvas.plot(("x", "y0", "y1", "y2"), name="plot 1", color="red")
canvas.plot(("x2", "y3", "y4"), name="plot 2", color="green")
return canvas
Edit: An earlier response fixed the issue with a two-line modification, but it wasn't the ideal way to solve the problem.

y-axis with date

i have a graph to plot, with dates in each axis, in x axis i have a date that represent the date pictures of taks was taken, and in the y-axis i would like to put the finish dates of that task, my query returns something like this
Task PictureDate FinishDate
A 01-03-2010 20-05-2010
A 08-03-2010 20-05-2010
A 15-03-2010 22-05-2010
B 01-03-2010 12-06-2010
B 08-03-2010 13-06-2010
B 15-03-2010 14-06-2010
I can not put the Finishdate in y-axis, is there a workaround i can use to display y-axis label as date?
I don't know if it could apply for this, but in .Net we can use a DateTime as a double by doing.
Datetime dt = new DateTime()
double l = dt.toOADate();
dt = Datetime.FromOADate(double l);
Maybe by giving a format or axistype or something like this you can do it.