sách gpt4 ai đã đi

Đường đồng mức Matplotlib quá mỏng khi chiều rộng < 1.0, quá dày khi chiều rộng >= 1.0

In lại 作者:行者123 更新时间:2023-12-01 00:11:59 33 4
mua khóa gpt4 Nike

My bar chart

为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗?

nhập matplotlib.pyplot dưới dạng plt
import matplotlib.dates as mdates
nhập pandas dưới dạng pd
nhập numpy dưới dạng np

working_runs = pd.DataFrame(np.random.uniform(1, 2, 210),
columns=['distance'],
index=pd.date_range('2019-06-01', periods=210, freq='D'))
summed = working_runs['distance'].resample('W').sum()
df = pd.DataFrame(summed)

fig, ax = plt.subplots()

ax.bar(df.index, df.distance)
ax.set_xticks(df.index)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%B %d"))
ax.xaxis.set_minor_formatter(mdates.DateFormatter("%B %d"))
plt.xticks(rotation=90)

fig = ax.get_figure()

fig.set_figheight(10)
fig.set_figwidth(12)

plt.title('2019 Weekly Running Miles')
plt.ylabel('Distance /m')

fig.savefig("output.png")

我尝试像这样更改它:

ax.bar(df.index, df.distance,width=1)

0.9 看起来没有任何不同,1.0 看起来像这样:

thicker lines

1 Câu trả lời

我可以确认奇怪的行为,当将宽度设置为小于 1.0 时,它似乎被解释为一天的宽度。当将其设置为 1.0 或更高时,它会被解释为一周的宽度。

这似乎是 pandas 和 matplotlib 如何协同工作的问题。

解决方法可能是使用 edgecolorgiống ax.bar(df.index, df.distance, width=1, edgecolor='white')giống:

nhập matplotlib.pyplot dưới dạng plt
import matplotlib.dates as mdates
nhập pandas dưới dạng pd
nhập numpy dưới dạng np
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

working_runs = pd.DataFrame(np.random.uniform(1, 2, 210),
columns=['distance'],
index=pd.date_range('2019-06-01', periods=210, freq='D'))
summed = working_runs['distance'].resample('W').sum()
df = pd.DataFrame(summed)

fig, ax = plt.subplots()
ax.bar(df.index, df.distance, width=1, edgecolor='white')
ax.xaxis.set_major_formatter(mdates.DateFormatter("%B %d"))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))
ax.autoscale(enable=True, axis='x', tight=True)
plt.xticks(rotation=90)

plt.title('2019 Weekly Running Miles')
plt.ylabel('Distance /m')
plt.hiển thị()

example plot

我尝试使用 df.plot.bar(y='distance', width=0.9, ax=ax) 留在 pandas 中。格式化日期可以通过显式转换 chỉ số 来完成到标签列表。同样在这种情况下,使用 width=1 会使绘图看起来更好。和edgecolor='white' .

nhập matplotlib.pyplot dưới dạng plt
import matplotlib.ticker as mticker
nhập pandas dưới dạng pd
nhập numpy dưới dạng np

working_runs = pd.DataFrame(np.random.uniform(1, 2, 210),
columns=['distance'],
index=pd.date_range('2019-06-01', periods=210, freq='D'))
summed = working_runs['distance'].resample('W').sum()
df = pd.DataFrame(summed)

fig, ax = plt.subplots()
df.plot.bar(y='distance', width=0.9, ax=ax)
plt.xticks(range(len(df.index)),
[t.to_pydatetime().strftime("%b %d") for t in df.index],
rotation=90)
plt.title('2019 Weekly Running Miles')
plt.ylabel('Distance /m')
plt.hiển thị()

example with pandas plot.bar

关于python - 当宽度 <1.0 时,Matplotlib 周线太细;当宽度>=1.0 时,周线太粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577778/

33 4 0
Bài viết được đề xuất: r - 将 geom_area 的区域调整为 geom_line 的一条线
Bài viết được đề xuất: Java mysql语法错误
Bài viết được đề xuất: Eclipse 如何禁用对 txt 文件的验证 (lsp4e)
Bài viết được đề xuất: jQuery UI 对话框添加内容
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com