I am relatively new to coding, so I apologize beforehand for this simple question:
I want to plot 2week candlesticks.
After I resampled my dataset in 2 week chunks I plotted the results. Unfortunately, matplotlib plots the chart with the complete date range, meaning that there are 14 day gaps between each candle. I already have tried to use ax.xaxis.set_major_locator(WeekdayLocator(byweekday=MO, interval=2)) but this just formats the labels of the x-axis, not the used values.
The Code:
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_formatter(weekFormatter)
candlestick_ohlc(ax, zip(mdates.date2num(quotes.index.to_pydatetime()),
quotes['open'], quotes['high'],
quotes['low'], quotes['close']),
width=0.6)
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()
Heres the result:
So how can I create a continuous graph where the candlesticks are closer to each other ?
[EDIT]
Wow, the simple solution is to put the width higher... I am really sorry for this. It was my first post here :D
I think the problem is with minor locator, which makes smaller marks on x axis every day. You can use ax.xaxis.minorticks_off() to disable them.
[EDIT]
Hmm, now that I reread the question I think that you want candlesticks to be wider. There is width parameter to do just so.
Related
I am trying to plot line charts for both nighttime and daytime to compare the differences in traffic volume in both time periods.
plt.subplot(2,1,1) #plot in grid chart to better compare differences
by_hour_business_night['traffic_volume'].plot.line()
plt.title('Business Nights Traffic Volume by Hours')
plt.ylabel('Traffic Volume')
plt.ylim(0,6500)
plt.show()
The chart for nighttime shows up alright, but the xtick labels are in [0,5,10,15,20,25], how can I change the labels to fit the hours? Something along the lines like: [0,1,2,3,4,5,6,19,20,21,22,23]
I have tried
x=[0,1,2,3,4,5,6,19,20,21,22,23]
plt.xticks(x)
But then I just got [0-6] on the left, and [19-23] on the right, both crammed on either side, leaving the middle of the xticks blank.
Or is there a better way to plot the chart? Since there will be a breaking point between 6 and 19 hours, is there a way to avoid this?
I am new to python and matplotlib, so forgive me if my wordings aren't precise enough.
xticks takes in two arguments: an array-like object of the placements and an array-like object of the labels. So you can do something like this:
plt.xticks(x, x)
This will set a label equal to the placement of the xtick. For more info you can read the docs for xtick here
I have a bar graph which has datetimes on the x-axis. How can I add gaps between x-ticks with respect to the timedelta between each neighbour ticks? The solution does not have to be a bargraph, I can use any kind of graph which satisfies my problem.
For these kind of problems you may set x axis as date field. This will fill in na dates and leave gaps accordingly which is helpful to analyze time series and things like monthly spending and it's quite easy to do. Following code snipper should help.
ax = plt.subplot(111)
ax.bar(x, y, width=10)
ax.xaxis_date()
plt.show()
I have following question, and the title basically says it all:
I have plotted yearly data with matplotlib:
Plot for yearly data
Since this data spans a course of over 5 years, it will be quite messy and confusing to read the plot.
So my question is if there is an option to achieve something along the lines of this:
Plot with yearly markers
This would of course be practical do achieve on a monthly or yearly base.
I was looking for a solution for this, but since english is not my native language it is a bit hard to put my question into words.
dfx=data2["Date"]
dfx = pd.to_datetime(dfx, errors='coerce')
data2['dfx']=dfx
weekly_data = data2.groupby("stuff").resample('W',
on='dfx').sum().reset_index().sort_values(by='dfx')
weekly_data.set_index("dfx",inplace = True)
fig, ax = plt.subplots(figsize=(65,17))
#set ticks every week
ax.xaxis.set_tick_params(reset=True)
ax.xaxis.set_major_locator(mdates.MonthLocator())
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %Y'))
weekly_data["stuff"].plot(x_compat=True,ax=ax,marker='o',markerfacecolor="red")
plt.title(ex)
plt.xlabel("Date")
plt.ylabel("Stuff")
weekly_data["Stuff"].plot(x_compat=True, ax=ax)
I had to remove some of the lines of plot for confidentiality reasons.
I would be thankful for any input on this matter.
Best regards
You might be looking for something like matplotlib.pyplot.vlines, which allows you to create vertical lines at certain x-values. For more information, visit the documentation, which can be found here: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.vlines.html
Hi guys sorry if bad question but I have this histogram (pic attached) I need to somehow in my code include 'tick marks' up the horizontal axis as well as numbers 0,2,4,,6,8,10, 10 being the maximum in my example.
I have no idea how to add these tick marks I cannot use imports, dicts, anything like that. The closest idea that I have is some loop including
i * ([(max-min)/5])
where max and min are the beginning and end of the horizontal axis.
Have been staring at this for over a week and this is the final piece and I am drawing a blank so any help would be very appreciated!
To customise the appearance of the ticks, you can use the Axes.tick_params() method.
ex:
fig, ax = plt.subplots()
ax.plot(datasetname['Bronze'],women_degrees['Craft'],label='datasetname')
ax.tick_params(bottom="on", top="off", left="on", right="off")
plt.show()
I have dates for tick marks on my x-axis. How can I make them automatically not overlap?
There are a lot of S.O. questions and posts about setting the tick interval - but this won't work for me since the date range for my plot can vary from 1 week, up to 1 year.
When people have plots with highly variable ranges, what is the method to make the x-axis ticks automatically not overlap?
plt.plot(date_list, unique_list)
plt.ylabel('# Uniques per day')
You could rotate the xticks by 90 degrees (or any other value):
plt.xticks(rotation=90)
maybe you need to call tight_layout() if the ticks are then out of the frame
plt.tight_layout()
Don't have enough rep to comment, because I don't think the tip I am offering could solve this problem, but I had exactly the same issue.
My solution is to make the plot itself flatter, by
plt.figure(figsize = (20,6)).
My reasoning is as such, there are only so many pixels and if changing the date format and rotation as suggested by others is not an option, maybe stretch the plot a bit?