Python - x-axis labels not lining up with tick marks - python

I've successfully created the code to generate a bunch of charts. However, the x axis labels are slightly offset (to the left) from the x axis tick marks.
Dataframe
stationId date variable value prefix uom
0 site 1 2016-04-07 pH 6.90 NaN pH
1 site 1 2016-07-11 pH 6.80 NaN pH
2 site 1 2017-10-09 pH 6.80 NaN pH
3 site 1 2017-10-09 pH 6.80 NaN pH
4 site 1 2016-06-29 pH 6.79 NaN pH
Full dataframe here
There is nothing in the code which i can see why this should happen.
#plot
for line,group in linedf.groupby(['variable']):
x = group['date']
ax1 = group.plot(x='date', figsize=(8.2,4.5),linestyle='--',
linewidth=0.75,rot=0,marker='o',markersize=3)
#set axis labels and chart title
plt.title("chartTitle", fontsize=12)
ax1.set_xlabel('Date', fontsize=10)
ax1.set_ylabel('GWL (mAHD)',fontsize=10)
#set text font
rcParams['font.family'] = 'serif'
rcParams['font.serif'] = ['Cambria']
#set dates for x tick labels
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
yearsFmt = mdates.DateFormatter('%Y')
lgd = plt.legend(bbox_to_anchor=(0.0 ,-0.13, 1.0, -0.03),
loc=2,ncol = 6, mode="expand", borderaxespad=0.0,shadow=True)
plt.show()

Without seeing the dataframe you are using (or at least a chunk of it) I have to speculate a bit, but it should suffice to simply adjust the alignment of the tick labels manually using
for tick in ax1.xaxis.get_major_ticks():
tick.label1.set_horizontalalignment('center')
Without the dataframe I can't test to ensure this works in your case, but from the plot in the question it appears the alignment of the x-tick labels has been set to 'right' and setting them to 'center' will align them how you desire.
    Drawn from the centered ticklabels example in the matplotlib docs.

Related

Change plotly bar chart x axis from weeks to days

I have a table that I'm currently trying to display on a bar chart. It is annual data, with various data from the 1st/jan of one year until the 31st/dec of the same year
DATE COUNT
0 2019-01-01 42
1 2019-02-01 3
2 2019-03-01 31
3 2019-04-01 13
4 2019-05-01 1
...
When I plot this with 'date' as the x-axis, plotly is automatically converting the x axis to weeks, so that i have 52 bars instead of 365.
fig = px.histogram(df, x="DATE", y="COUNT", title="title")
fig.update_layout(bargap=0.30)
fig
I've tried updating the ticks with various formats, but this just changes the x axis labels, not the number of bars
I'm not sure how to change it from weekly to daily on the x-axis

Pandas: Plotting / annotating from DataFrame

There is this boring dataframe with stock data I have:
date close MA100 buy sell
2022-02-14 324.95 320.12 0 0
2022-02-13 324.87 320.11 1 0
2022-02-12 327.20 321.50 0 0
2022-02-11 319.61 320.71 0 1
Then I am plotting the prices
import pandas as pd
import matplotlib.pyplot as plt
df = ...
df['close'].plot()
df['MA100'].plot()
plt.show()
So far so good...
Then I'd like to show a marker on the chart if there was buy (green) or sell (red) on that day.
It's just to highlight if there was a transaction on that day. The exact intraday price at which the trade happened is not important.
So the x/y-coordinates could be the date and the close if there is a 1 in column buy (sell).
I am not sure how to implement this.
Would I need a loop to iterate over all rows where buy = 1 (sell = 1) and then somehow add these matches to the plot (probably with annotate?)
I'd really appreciate it if someone could point me in the right direction!
You can query the data frame for sell/buy and scatter plot:
fig, ax = plt.subplots()
df.plot(x='date', y=['close', 'MA100'], ax=ax)
df.query("buy==1").plot.scatter(x='date', y='close', c='g', ax=ax)
df.query("sell==1").plot.scatter(x='date', y='close', c='r', ax=ax)
Output:

How to plot evenly spaced values on the x axis while plotting using matplotlib

I have a Series with more than 100 000 rows that I want to plot. I have problem with the x-axis of my figure. Since my x-axis is made of several dates, you can't see anything if you plot all of them.
How can I choose to show only 1 out of every x on the x-axis ?
Here is an example of a code which produces a graphic with an ugly x-axis :
sr = pd.Series(np.array(range(15)))
sr.index = [ '2018-06-' + str(x).zfill(2) for x in range(1,16)]
Out :
2018-06-01 0
2018-06-02 1
2018-06-03 2
2018-06-04 3
2018-06-05 4
2018-06-06 5
2018-06-07 6
2018-06-08 7
2018-06-09 8
2018-06-10 9
2018-06-11 10
2018-06-12 11
2018-06-13 12
2018-06-14 13
2018-06-15 14
fig = plt.plot(sr)
plt.xlabel('Date')
plt.ylabel('Sales')
Using xticks you can achieve the desired effect:
In your example:
sr = pd.Series(np.array(range(15)))
sr.index = [ '2018-06-' + str(x).zfill(2) for x in range(1,16)]
fig = plt.plot(sr)
plt.xlabel('Date')
plt.xticks(sr.index[::4]) #Show one in every four dates
plt.ylabel('Sales')
Output:
Also, if you want to set the number of ticks, instead, you can use locator_params:
sr.plot(xticks=sr.reset_index().index)
plt.locator_params(axis='x', nbins=5) #Show five dates
plt.ylabel('Sales')
plt.xlabel('Date')
Output:

Add In Plot Labels to Seaborn Lineplot

I have a dataframe that shows monthly revenue. There is an additional column that shows the number of locations opened in that month.
> Date Order Amount Locations Opened
16 2016-05-31 126443.17 2.0
> 17 2016-06-30 178144.27 0.0
18 2016-07-31 230331.96 1.0
> 19 2016-08-31 231960.04 0.0
20 2016-09-30 208445.26 0.0
I'm using seaborn to plot the revenue by month
sns.lineplot(x="Date", y="Order Amount",
data=total_monthly_rev).set_title("Total Monthly Revenue")
I've been trying, unsuccessfully, to use the third column, Locations Opened, to add supporting text to the lineplot so I can show the number of locations opened in a month, where Locations Opened > 0.
IIUC, use text:
plt.figure(figsize=(12, 5))
sns.lineplot(x="Date", y="Order Amount", data=total_monthly_rev).set_title("Total Monthly Revenue")
# Using a variable to manage how above/below text should appear
slider = 1000
for i in range(total_monthly_rev.shape[0]):
if total_monthly_rev['LocationsOpened'].iloc[i] > 0:
plt.text(total_monthly_rev.Date.iloc[i],
total_monthly_rev['Order Amount'].iloc[i] + slider,
total_monthly_rev['LocationsOpened'].iloc[i])
plt.show()

How to change spacing between two ticks in matplotlib chart?

I'm plotting some data that requires Day 0 to not be shown on the x-axis. The dataframe has no column for Day 0, but Matplotlib creates a space for it between day -1 and 1. I've looked through the documentation, but can't find a way to adjust spacing between only two ticks. The dataframe is:
group stat -1.0 1.0 2.0 3.0 4.0 5.0
abc mean 8.362999 17.043362 3.526539 22.931884 10.835121 6.035011
abc sem 1.481135 5.029173 0.822778 13.768812 2.149704 0.840965
abc std 3.311919 11.245573 1.839788 30.787999 4.806885 1.880455
Code to plot:
df.set_index(['subject'], inplace=True)
df.drop(['group'],axis=1,inplace=True)
x = df.columns.values
y = df.loc['mean'].values
sem = df.loc['sem'].values
plt.errorbar(x, y, sem, color='#0075d9', marker='o', clip_on=False)
This is an example of the chart (please ignore the shading):
You can see that it has more space between -1 and 1 than the other ticks. Is there a way to 'drop' the Day 0 tick from the X-axis?

Categories

Resources