Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: run black linter #3

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: lint
on: [push, pull_request]
jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: Install Python dependencies
run: pip install black flake8

- name: Run linters
uses: wearerequired/lint-action@v2
with:
auto_fix: false
black: true
flake8: false
115 changes: 67 additions & 48 deletions hadcrut5-bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,54 @@

from hadcrut5lib import argparser, HadCRUT5


def parse_args():
"""This function parses and return arguments passed in"""
descr = "Parse and plot the HadCRUT5 temperature datasets"
examples = [
"%(prog)s",
"%(prog)s --period \"1850-1900\"",
"%(prog)s --period \"1880-1920\"",
"%(prog)s --outfile HadCRUT5-global.png"]
"%(prog)s",
'%(prog)s --period "1850-1900"',
'%(prog)s --period "1880-1920"',
"%(prog)s --outfile HadCRUT5-global.png",
]

parser = argparser(descr, examples)
parser.add_argument(
"-f", "--outfile",
action="store", dest="outfile",
help="name of the output PNG file")
"-f",
"--outfile",
action="store",
dest="outfile",
help="name of the output PNG file",
)
parser.add_argument(
"-p", "--period",
action="store", dest="period", default="1961-1990",
help="show anomalies related to 1961-1990 (default), 1850-1900, or 1880-1920")
"-p",
"--period",
action="store",
dest="period",
default="1961-1990",
help="show anomalies related to 1961-1990 (default), 1850-1900, or 1880-1920",
)
parser.add_argument(
"-v", "--verbose",
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="make the operation more talkative")
help="make the operation more talkative",
)

return parser.parse_args()


def plotbar(period, outfile, verbose):
"""
Create a bar plot for the specified period and diplay it or save it to file
if outfile is set
"""

# pylint: disable=W0613
def major_formatter(x, pos):
return f'{x:.1f}' if x >= 0 else ''
return f"{x:.1f}" if x >= 0 else ""

# pylint: enable=W0613

hc5 = HadCRUT5(period=period, verbose=verbose)
Expand All @@ -57,67 +71,72 @@ def major_formatter(x, pos):

bar_width = 0.7
basefont = {
'family': 'DejaVu Sans',
'color' : 'white',
'weight': 'bold',
"family": "DejaVu Sans",
"color": "white",
"weight": "bold",
}
fontxl = { **basefont, "size": 20 }
fontxs = { **basefont, "size": 12 }
fontxl = {**basefont, "size": 20}
fontxs = {**basefont, "size": 12}

plt.style.use("dark_background")
_, ax = plt.subplots()

cmap = plt.cm.jet # or plt.cm.bwr
cmap = plt.cm.jet # or plt.cm.bwr
norm = matplotlib.colors.Normalize(vmin=-1, vmax=max(mean))
colors = cmap(norm(mean))

ax.bar(years,
mean,
width=bar_width,
color=colors,
align="center")
ax.bar(years, mean, width=bar_width, color=colors, align="center")

ax.set_frame_on(False)
ax.yaxis.tick_right()
ax.yaxis.set_major_formatter(major_formatter)
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.tick_params(axis="both", which="both", length=0)

upper, left = .95, .025
upper, left = 0.95, 0.025
last_year = years[-1]

text_props = dict(horizontalalignment="left",
verticalalignment="top",
transform=ax.transAxes)
plt.text(left, upper,
'\n'.join((
r"Global average temperature difference *",
r"1850-{}".format(last_year))),
fontdict=fontxl,
linespacing=1.2,
**text_props)
plt.text(left, upper - .125,
'\n'.join((
r"(*) Compared to {} pre-industrial levels".format(period),
r"Data source - HadCRUT5")),
fontdict=fontxs,
linespacing=1.5,
**text_props)
text_props = dict(
horizontalalignment="left", verticalalignment="top", transform=ax.transAxes
)
plt.text(
left,
upper,
"\n".join(
(r"Global average temperature difference *", r"1850-{}".format(last_year))
),
fontdict=fontxl,
linespacing=1.2,
**text_props,
)
plt.text(
left,
upper - 0.125,
"\n".join(
(
r"(*) Compared to {} pre-industrial levels".format(period),
r"Data source - HadCRUT5",
)
),
fontdict=fontxs,
linespacing=1.5,
**text_props,
)

fig = plt.gcf()
fig.set_size_inches(10, 8) # 1 inch equal to 80pt
fig.set_size_inches(10, 8) # 1 inch equal to 80pt

if outfile:
fig.savefig(outfile, dpi=80, bbox_inches='tight')
fig.savefig(outfile, dpi=80, bbox_inches="tight")
plt.close(fig)
else:
plt.show()


# pylint: disable=C0116
def main():
args = parse_args()

plotbar(args.period,
args.outfile,
args.verbose)
plotbar(args.period, args.outfile, args.verbose)


main()
Loading
Loading