我一直在浏览 tqdm文档,但无论我在哪里看,我都找不到一种方法来提取经过的时间和估计的剩余时间字段(基本上是每行进度条的中心:)00:00<00:02。
0%| | 0/200 [00:00<?, ?it/s]
4%|? | 7/200 [00:00<00:02, 68.64it/s]
8%|? | 16/200 [00:00<00:02, 72.87it/s]
12%|?? | 25/200 [00:00<00:02, 77.15it/s]
17%|?? | 34/200 [00:00<00:02, 79.79it/s]
22%|??? | 43/200 [00:00<00:01, 79.91it/s]
26%|??? | 52/200 [00:00<00:01, 80.23it/s]
30%|??? | 61/200 [00:00<00:01, 82.13it/s]
....
100%|??????????| 200/200 [00:02<00:00, 81.22it/s]
Run Code Online (Sandbox Code Playgroud)
tqdm基本上通过在发生更新时打印动态进度条来工作,但是有没有办法“只”打印00:01和00:02部分,所以我可以在我的 Python 程序的其他地方使用它们,例如在自动停止代码中,如果它停止进程花的时间太长了吗?
我一直是 R 的长期用户,最近过渡到 Python,并且我一直在尝试使用 ggplot2 进行绘图,因为它非常直观。Plotnine被认为是最 ggplot2 风格的绘图库,我已经成功地用它重新创建了大多数图形,除了关键的是如何绘制常规函数。
在基础 R 中,您可以轻松定义一个方程,如so,将结果输入到一个stat_function()层中,并设置图形的限制代替dataarg.,并成功绘制抛物线等。但是,设置图形限制的语法在 Python 中必须不同(也许使用 numpy?),并且使用 定义方程sympy,这对我来说是另一个分歧。
那么我将如何使用 plotnine 绘制函数呢?以上两个障碍是我认为给我带来麻烦的与 ggplot2 的两个不同之处,因为 plotnine 在网上的例子太少了。
PS 这是我想使用 plotnine 在 Python 中重新创建的示例:
> library(ggplot2)
> basic_plot <- function(x) x^2 + 2.5
> graph <- ggplot(data.frame(x=c(-5,5)), aes(x=x)) +
+ stat_function(fun = basic_plot)
> graph
Run Code Online (Sandbox Code Playgroud) How would I scrape an entire website, with all of its css/javascript/media intact (and not just its HTML), with google's Puppeteer? After successfully trying it out on other scraping jobs, I would imagine it should be able to.
However, looking through the many excellent examples online, there is no obvious method for doing so. The closest I have been able to find is calling
html_contents = await page.content()
Run Code Online (Sandbox Code Playgroud)
and saving the results, but that saves a copy without any …
因此,我第一次开始使用 tkinter (8.6.7),通过 conda 安装到我的一个带有 python 3.5.4 的环境中,由于某些原因,它一直导致我的 jupyter 笔记本崩溃。这是我使用的简单代码,
from tkinter import filedialog
from tkinter import *
root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
Run Code Online (Sandbox Code Playgroud)
一旦我退出使用 Finder 打开的对话框窗口(我在 Mac 上),我的内核就会崩溃并重新启动,即使我没有选择任何文件并只按取消键。
有谁知道为什么这种情况不断发生?我的环境是最新的,可以使用 numpy,例如,没有问题。
我刚刚开始使用 PyCharm,因为我想更多地使用 Python(对其他语言有更多的经验),并且遇到了一个必须有解决方案的难题。我创建的某些自定义函数依赖于以特别冗长的方式导入的模块,因此我不想在每次编码时都复制粘贴相同的两行,而不是简单地键入import numpy as np.
那么有没有办法在我的环境启动时自动运行一些代码?特别是要导入一些模块?我环顾了 PyCharm 的设置,但一无所获。
我处于一种奇怪的情况,我需要一个 Python 函数从脚本中运行,然后从我的主代码中调用该脚本。
我想使用该subprocess模块,并知道如何使用它来将参数传递给纯脚本,但问题是,我需要将参数传递给其中的嵌套 Python 函数,其中大部分是可选的并且具有默认值。
我想arparse会以某种方式帮助我做到这一点。
这是我正在尝试的示例:
## Some Argparse, which will hopefully help
import argparse
parser = argparse.ArgumentParser()
## All arguments, with only "follow" being required
parser.add_argument('file_name', help='Name of resulting csv file')
parser.add_argument('sub_name', help='Sub-name of resulting csv file')
parser.add_argument('follow', help='Account(s) to follow', required=True)
parser.add_argument('locations', help='Locations')
parser.add_argument('languages', help='Languages')
parser.add_argument('time_limit', help='How long to keep stream open')
args = parser.parse_args()
## Actual Function
def twitter_stream_listener(file_name=None,
sub_name='stream_',
auth = api.auth,
filter_track=None,
follow=None,
locations=None,
languages=None,
time_limit=20):
... function code …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用plotnine 进行绘图,据我所知,它是Python 中最像ggplot2 风格的库,但我很难在Python 脚本中的PyCharm 中完成它。这似乎是一个问题,对于 matplotlib 和其他也遇到此错误的绘图库有许多解决方案,但使用他们的答案(添加DISPLAY=True到环境变量,show()在图末尾添加等)不适用于plotnine。
我正在尝试绘制以下内容,它在常规 ipython 窗口(我通过终端访问)中工作:
from plotnine import *
from plotnine.data import *
(ggplot(diamonds)
+ stat_summary(aes('clarity', 'price', fill='cut'), geom='col', position='dodge')
+ stat_summary(aes('clarity', 'price', color='cut'), geom='linerange', size=1, position=position_dodge(width=0.9))
+ scale_color_hue(l=.3)
+ theme(figure_size=(11, 8))
)
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么,或者我不应该尝试从脚本中进行绘图,这通常对其他一切都很方便?
PS 解决方案:这似乎是 PyCharm 中的一个错误,社区可能还没有注意到,但这有点公平,因为plotnine 库也有点新。我的解决方案是避免尝试在脚本中执行此操作,而是从 PyCharm 中的 ipython 控制台内进行绘图,这是有效的。
我想使用 seaborn 用 violinplots 来可视化我的整个 Pandas 数据框,并且我认为我已经进行了必要的更正,以便为我的数据框拥有的 270 个变量生成一个大图。
但是,无论我做什么,小提琴图都只显示每个变量的内部迷你箱线图(正如这里描述的另一个问题),而不是它们的 kde:
fig, ax = plt.subplots(figsize=(50,5))
ax.set_ylim(-6, 6)
a = sns.violinplot(x='variable', y='value', data=pd.melt(train_norm), ax=ax)
a.set_xticklabels(a.get_xticklabels(), rotation=90);
plt.savefig('massive_violinplot.png', figsize=(50,5), dpi=220)
Run Code Online (Sandbox Code Playgroud)
(为裁剪图道歉,整个事情太大而无法发布)
而以下代码,使用相同的pd.Dataframe,但只显示前六个变量,显示正确:
fig, ax = plt.subplots(figsize=(10,5))
ax.set_ylim(-6, 6)
a = sns.violinplot(x='variable', y='value', data=pd.melt(train_norm.iloc[:,:6]), ax=ax)
a.set_xticklabels(a.get_xticklabels(), rotation=90);
plt.savefig('massive_violinplot.png', figsize=(10,5), dpi=220)
Run Code Online (Sandbox Code Playgroud)
我怎么能得到像上面所有变量的图表,用适当的小提琴图来显示他们的kde?
我最近一直在尝试使用Dash构建应用程序,但是尽管浏览了许多指南,但我仍然无法弄清楚如何将熊猫数据框导入Dash的数据表(本质上是熊猫数据框,但Web托管和响应式除外) 。
大多数示例说明了如何从示例中已经进行硬编码的数据帧中手动选择某些列/行,如此处所示。但是,在我的情况下,数据帧是在代码内构建的(而pandas是实现此目的的最简单方法),因此我最终不得不找出一种将a转换pd.Dataframe()为a的方法dash_table.DataTable()。
我该如何进行这项工作?使用引用,我尝试了以下代码将我的数据帧的字典发送到dash_table.DataTable(),但没有任何显示。
我的代码:
## Imports
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
import datetime as dt
import pandas as pd
import numpy as np
## Custom functions that creates the pandas dataframe
from twitter_functions import old_tweets
app = dash.Dash(dev_tools_hot_reload=True)
app.scripts.config.serve_locally = True
app.config['suppress_callback_exceptions'] = True
app.layout = html.Div(children=[
html.H3('Twitter App'),
dcc.Input('ScreenName_Input', type='text'),
html.Button(id='screenNames_submit_button', children='Submit'),
dash_table.DataTable(id='tweet_table')
])
@app.callback( …Run Code Online (Sandbox Code Playgroud) python ×8
pandas ×2
plot ×2
plotnine ×2
pycharm ×2
argparse ×1
automation ×1
bash ×1
crash ×1
css ×1
ggplot2 ×1
html ×1
iterable ×1
javascript ×1
jupyter ×1
matplotlib ×1
plotly-dash ×1
printing ×1
progress-bar ×1
puppeteer ×1
r ×1
seaborn ×1
subprocess ×1
tkinter ×1
tqdm ×1
web-scraping ×1