Bài viết phổ biến của tác giả
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我的条形这么细?我尝试将宽度设置为 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 看起来像这样:
1 Câu trả lời
我可以确认奇怪的行为,当将宽度设置为小于 1.0 时,它似乎被解释为一天的宽度。当将其设置为 1.0 或更高时,它会被解释为一周的宽度。
这似乎是 pandas 和 matplotlib 如何协同工作的问题。
解决方法可能是使用 edgecolor
giố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ị()
我尝试使用 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ị()
关于python - 当宽度 <1.0 时,Matplotlib 周线太细;当宽度>=1.0 时,周线太粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59577778/
Tôi là một lập trình viên xuất sắc, rất giỏi!