I'd like to be able to plot stock data on my chart without any gaps where data is missing.
This answer had a way of doing it like this:
# the dates in my example file-set are very sparse (and annoying) change the dates to be sequential
for i in range(len(r)-1):
r['date'][i+1] = r['date'][i] + datetime.timedelta(days=1)
I didn't understand how or why that worked but tried to implement it in my code like this:
for i in range(len(date)-1):
date[i+1] = date[i] + dt.timedelta(days=1)
Here is the code for my chart:
fig = plt.figure()
fig.set_size_inches(13.5, 8.5)
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=4, colspan=1)
ax1.yaxis.tick_right()
ax1.xaxis.set_ticks_position('bottom')
plt.title('title')
# Add a seconds axis for the volume overlay
ax2 = plt.subplot2grid((6,1), (4,0), rowspan=4, colspan=1)
ax2.xaxis.set_ticks_position('bottom')
stock_price_url = 'https://www.quandl.com/api/v3/datasets/WIKI/AAPL/data.csv?start_date=2015-06-01&order=asc&end_date=2015-08-01&collapse=daily'
source_code = urllib.urlopen(stock_price_url).read().decode()
stock_data = []
split_source = source_code.split('\n')
for line in split_source:
split_line = line.split(',')
if 'Date' not in line:
stock_data.append(line)
date, openp, highp, lowp, closep, volume = np.loadtxt(stock_data,
delimiter=',',
unpack=True,
converters={0:strpdate2num('%Y-%m-%d')},
usecols=(0,1,2,3,4,5))
x = 0
y = len(date)
ohlc = []
while x < y:
append_me = date[x], openp[x], closep[x], highp[x], lowp[x], volume[x]
ohlc.append(append_me)
x+=1
candlestick(ax1, ohlc, width=0.4, colorup='g', colordown='r')
# create the second axis for the volume bar-plot
ax2.yaxis.tick_right()
ax2.yaxis.get_major_formatter().set_scientific(False)
# set the position of ax2 so that it is short (y2=0.32) but otherwise the same size as ax
ax2.set_position(mpl.transforms.Bbox([[0.125,0.1],[0.9,0.32]]))
# get data from candlesticks for a bar plot
dates = [x[0] for x in ohlc]
dates = np.asarray(dates)
volume = [x[5] for x in ohlc]
volume = np.asarray(volume)
# make bar plots and color differently depending on up/down for the day
pos = openp-closep<0
neg = openp-closep>0
ax2.bar(dates[pos],volume[pos],color='green',width=1,align='center')
ax2.bar(dates[neg],volume[neg],color='red',width=1,align='center')
#scale the x-axis tight
ax1.set_xlim(min(dates),max(dates))
ax2.set_xlim(min(dates),max(dates))
# the y-ticks for the bar were too dense, keep only every third one
yticks = ax2.get_yticks()
ax2.set_yticks(yticks[::3])
# format the x-ticks with a human-readable date.
xt = ax1.get_xticks()
new_xticks = [dt.date.isoformat(num2date(d)) for d in xt]
ax1.set_xticklabels(new_xticks,rotation=45, horizontalalignment='right')
ax2.set_xticklabels(new_xticks,rotation=45, horizontalalignment='right')
plt.show()
Any help would be really awesome.
Related
I am trying to align X axis with its twin but I'm not finding a way to do it.
Here is my code
# Initialize the figure
plt.figure(figsize=(16, 10))
# Adding a title
plt.title(f'Client Retention Quarters: Monthly Cohorts', fontsize = 14)
# Creating the heatmap
sns.heatmap(retention, annot = True,vmin = 0, vmax =30,cmap="flare", fmt='g')
plt.ylabel('Cohort Quarter')
plt.xlabel('')
plt.yticks( rotation='360')
#Twinx
ax2 = plt.twiny()
ax2.set_xticks(range(0,len(x2)))
ax2.set_xticklabels(labels=x2)
ax2.spines['top'].set_position(('axes', -0.10))
plt.show()
And here is the output:
I want to align the percentages with the x ticks.
Is it possible?
You can use the below updated code. See if this works. Note that I have used random data for retention and x2. Basically, the main change it to get the xlim()s for both axes and then adjust it (see lambda f) so that the ticks align. Finally use set_major_locator() to fix the points. Hope this is what you are looking for...
retention = np.random.rand(10, 12) ##My random data
# Initialize the figure
plt.figure(figsize=(16, 10))
# Adding a title
plt.title(f'Client Retention Quarters: Monthly Cohorts', fontsize = 14)
# Creating the heatmap
ax=sns.heatmap(retention, annot = True,vmin = 0, vmax =30,cmap="flare", fmt='g') ## Note I am assigning to ax
plt.ylabel('Cohort Quarter')
plt.xlabel('')
plt.yticks( rotation='360')
x2 = np.around(np.linspace(1, 25, 12),2)
#Twinx
ax2 = ax.twiny()
#ax2.set_xticks(range(0,len(x2))) ## Commented as not required
#ax2.set_xticklabels(labels=x2) ## Commented as not required
## New code here ##
import matplotlib.ticker
l = ax.get_xlim()
l2 = ax2.get_xlim()
f = lambda y : l2[0]+(y-l[0])/(l[1]-l[0])*(l2[1]-l2[0]) ##Add delta to each tick
ticks = f(ax.get_xticks())
ax2.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(ticks)) ##Set the ticks
ax2.spines['top'].set_position(('axes', -0.10))
plt.show()
I am attempting to plot multiple line graphs in a graph table itself. However, I run into an error that mentioned:
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
Not only this happened but my legend tables of the 3 lines don't merge together and my X-axis does not show the months but random numbers from my dataframe. Here is my code and graph result to look through.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.read_excel (r'C:\Users\admin\Desktop\Question Folder\Sales of top 30 customers.xlsx')
#Refine and adjust the dataframe for suitable manipulation
df = df.drop('Unnamed: 0', axis = 1)
df = df.iloc[2: , :]
row_detail = df.head(1).values.tolist()
row_detail = row_detail[0]
a = df.iloc[-3:, :].values.tolist()
a = a[0]
df.columns = row_detail
df = df.iloc[1:, :]
print(df) # This is for checking purpose
# This creates a dataframe needed for the practice
df1 = df.iloc[:3]
# This is to plot a line graph from df1
df_chosen = df1
a = 0
# Turning data row of a customer into a list
data_row_1 = df_chosen.iloc[a].values.tolist()
data_row_2 = df_chosen.iloc[a + 1].values.tolist()
data_row_3 = df_chosen.iloc[a + 2].values.tolist()
date = data_row_1[1:]
cus_1 = data_row_1[0]
cus_2 = data_row_2[0]
cus_3 = data_row_3[0]
y1 = data_row_1[1:]
y2 = data_row_2[1:]
y3 = data_row_3[1:]
x = np.arange(len(date)) # the label locations
width = 0.60 # the width of the bars
fig, ax = plt.subplots()
# Increase size of plot in jupyter
plt.rcParams["figure.figsize"] = (20,15)
plt.rcParams.update({'font.size':25})
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_xlabel('Months', fontsize=30)
ax.set_ylabel('Sales', fontsize=30)
ax.set_title('Monthly Sales from ' + cus_1 +", " + cus_2+ " and " + cus_3, fontsize=30)
ax.set_xticks(x, date)
ax.set_ylim(bottom = 0, top = 1000)
legend1 = plt.legend(())
ax.legend(loc='best', fontsize=30)
plt.grid(True)
# set up the 1st line graph
ax.plot(x, y1, "r", label = cus_1, marker='x')
#ax.set_yticks(
ax.grid(True) # turn on grid #1
ax.set_ylim(bottom = 0, top = 1000)
ax.legend(loc='upper left', fontsize=25)
ax2 = ax.twinx()
ax2.plot(x, y2, "b", label= cus_2, marker='x')
ax2.set_yticks([])
ax2.grid(False) # turn off grid #2
ax2.set_ylim(bottom = 0, top = 10000)
ax2.legend(loc='upper left', fontsize=25)
ax3 = ax2.twinx()
ax3.plot(x, y3, "g", label= cus_3, marker='x')
ax3.set_yticks([])
ax3.grid(False) # turn off grid #2
ax3.set_ylim(bottom = 0, top = 10000)
ax3.legend(loc='upper left', fontsize=25)
I just need to understand and know the solutions for the following:
Why is the X-axis not showing the months' names?
Why is the 3 separate legend tables not connected together?
How do I avoid the 'No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.' error warning?
Hope to receive a favorable reply soon. :)
Edit notice: Here is the dataframe used for this problem:
I am able to produce the following plot with this code:
df_day['Time'] = pd.to_datetime(df_day['Time'], format = '%H:%M:%S')
df_day = df_day.set_index('Time')
fig=plt.figure(dpi=900)
plt.title("TESTER Summary for Day %i " %y + "\n Average Chamber Pressure %.3f [torr] \n" %p_avg_temp + str(dates[x]))
ax1 = df_day['DP-2'].plot(label = 'DP-2')
ax2 = df_day['FM-1'].plot(secondary_y = True, label = "Flow Rate")
ax1.set_ylabel('Temperatures - Pressure Drop\nInlet Pressure - Scale Weight')
ax2.set_ylabel('Flow Rate - Heat Rej.')
ax1.set_xlabel('Time ')
handles,labels = [],[]
for ax in fig.axes:
for h,l in zip(*ax.get_legend_handles_labels()):
handles.append(h)
labels.append(l)
plt.legend(handles, labels, loc = 'lower center', bbox_to_anchor = (0.5, -0.5), ncol = 3)
fig.subplots_adjust(bottom = 0.25)
ax1.grid(True, linestyle = ':')
ax2.grid(True, linestyle = ':')
plt.show()
But I don't want the date to show up in the xtick labels. I want hours, minutes and seconds. I have also tried:
df_day['Time'] = pd.Series([val.time() for val in df_day['Time']])
instead of
df_day['Time'] = pd.to_datetime(df_day['Time'], format = '%H:%M:%S')
but then I get
and the time intervals are arbitrary and don't include seconds when the seconds are 00.
Try running this lines before you code:
import matplotlib.dates as mdates
myFmt = mdates.DateFormatter('%H:%M:%S') # here you can format your datetick labels as desired
plt.gca().xaxis.set_major_formatter(myFmt)
And look at this post plot_date function set xticks for hourly data
I plot a chart with matplotlib but the x-ticks are too crowded. May I know any solution to fix it?
from pandas_datareader import data
import datetime
tickers = 'AAPL'
dateToday = datetime.datetime.today().strftime("%Y-%m-%d")#年月日20190526
# Only get the adjusted close.
tickers_data = data.DataReader(tickers,
start='',
end=dateToday,
data_source='yahoo')[["Adj Close", "Volume"]][-250:]
returns = tickers_data.pct_change()
plt.figure(figsize=(12,6))
ax = sns.barplot(x=returns.index.strftime('%d/%-m'), y=returns['Adj Close'], color='#73a9d1')
plt.xticks(rotation = 90)
plt.title('Returns' + '\n' + tickers)
Output:
If you want to see, for example, every fifth x-tick (x-tick step is 5), you can improve your code in this way:
step = 5
x_values = returns.index.strftime('%d/%-m')
x_ticks_values = x_values[::step]
plt.figure(figsize = (12, 6))
ax = sns.barplot(x = x_values,
y = returns['Adj Close'],
color = '#73a9d1')
plt.xticks(ticks = np.arange(0, (len(x_values) + step), step),
labels = x_ticks_values,
rotation = 90)
Currently I have a plot with too many points, I want to avoid overlapping. Want to know how to reduce the amount of points in order to have a smoother line.
Plot Code
fig = plt.figure(1, figsize = (18,10)) # Figure size in inches (size_x, size_y)
ax = plt.axes()
min_val = prediction_intervals2[:, 0]
max_val = prediction_intervals2[:, 1]
true_values = y_test
predicted_values = PLS_Model1.predict(X_test)
plt.plot(min_val, label = "Min", color='blue')
plt.plot(max_val, label = "Max", color='red')
plt.plot(true_values, label = "y", color = "black")
plt.plot(predicted_values, label = "y\u0302", marker='o')
plt.title('Conformal Predictor Final Predictions')
plt.legend()
plt.show()
Current Plot
Desired Plot
Plot that I want
I was able to revise my code properly and came to the desired output by just selecting less data points, quite simple. Posted the answer just in case.
min_val_normal = plot_normalized_table[['Min']]
max_val_normal = plot_normalized_table[['Max']]
original_normal = plot_normalized_table[['Original Label']]
interval_normal = plot_normalized_table[['Interval Size']]
normal_predicted = predicted_values[0:50]
fig = plt.figure(1, figsize = (18,10)) # Figure size in inches (size_x, size_y)
ax = plt.axes()
#predicted_values = PLS_Model1.predict(X_test) #Predictions from test data (run at least once for the plot to work)
plt.plot(min_val_normal, label = "Min", color='blue')
plt.plot(max_val_normal, label = "Max", color='red')
plt.plot(original_normal, label = "y", color = "black")
plt.plot(normal_predicted, label = "y\u0302", marker='o', )
plt.title('Normalized Final Conformal Predictions')
plt.xlim([-1, 51])
plt.ylim([-1, 2])
plt.legend()
plt.show()