如何更改 matplotlib 中的 x 轴,以便没有空格? [英] How can I change the x axis in matplotlib so there is no white space?

查看:39
本文介绍了如何更改 matplotlib 中的 x 轴,以便没有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以目前正在学习如何导入数据并在 matplotlib 中使用它,即使我有书中的确切代码,我也遇到了麻烦.

这就是情节的样子,但我的问题是如何在 x 轴的起点和终点之间没有空白的地方得到它.

代码如下:

导入csv从 matplotlib 导入 pyplot 作为 plt从日期时间导入日期时间# 从文件中获取日期和高温.文件名 = 'sitka_weather_07-2014.csv'使用 open(filename) 作为 f:读者 = csv.reader(f)header_row = 下一个(读者)#for index, column_header in enumerate(header_row):#print(index, column_header)日期,高点 = [], []对于阅读器中的行:current_date = datetime.strptime(row[0], "%Y-%m-%d")日期.追加(current_date)高 = 整数(行 [1])highs.append(high)# 绘制数据.fig = plt.figure(dpi=128, figsize=(10,6))plt.plot(日期,高点,c='红色')# 格式化绘图.plt.title("每日高温,2014 年 7 月", fontsize=24)plt.xlabel('', fontsize=16)fig.autofmt_xdate()plt.ylabel("温度 (F)", fontsize=16)plt.tick_params(axis='both', which='major', labelsize=16)plt.show()

解决方案

在边缘设置了一个自动边距,可确保数据很好地适合轴脊.在这种情况下,y 轴上可能需要这样的边距.默认情况下,它设置为 0.05,以轴跨度为单位.

要将 x 轴上的边距设置为 0,请使用

plt.margins(x=0)

ax.margins(x=0)

取决于上下文.另请参阅


或者,使用 plt.xlim(..)ax.set_xlim(..) 手动设置轴的限制,以便没有空白.

So currently learning how to import data and work with it in matplotlib and I am having trouble even tho I have the exact code from the book.

This is what the plot looks like, but my question is how can I get it where there is no white space between the start and the end of the x-axis.

Here is the code:

import csv

from matplotlib import pyplot as plt
from datetime import datetime

# Get dates and high temperatures from file.
filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    #for index, column_header in enumerate(header_row):
        #print(index, column_header)
    dates, highs = [], []
    for row in reader:
        current_date = datetime.strptime(row[0], "%Y-%m-%d")
        dates.append(current_date)

        high = int(row[1])
        highs.append(high)

# Plot data. 
fig = plt.figure(dpi=128, figsize=(10,6))
plt.plot(dates, highs, c='red')


# Format plot.
plt.title("Daily high temperatures, July 2014", fontsize=24)
plt.xlabel('', fontsize=16)
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()

解决方案

There is an automatic margin set at the edges, which ensures the data to be nicely fitting within the axis spines. In this case such a margin is probably desired on the y axis. By default it is set to 0.05 in units of axis span.

To set the margin to 0 on the x axis, use

plt.margins(x=0)

or

ax.margins(x=0)

depending on the context. Also see the documentation.

In case you want to get rid of the margin in the whole script, you can use

plt.rcParams['axes.xmargin'] = 0

at the beginning of your script (same for y of course). If you want to get rid of the margin entirely and forever, you might want to change the according line in the matplotlib rc file:

axes.xmargin : 0
axes.ymargin : 0

Example

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
tips.plot(ax=ax1, title='Default Margin')
tips.plot(ax=ax2, title='Margins: x=0')
ax2.margins(x=0)


Alternatively, use plt.xlim(..) or ax.set_xlim(..) to manually set the limits of the axes such that there is no white space left.

这篇关于如何更改 matplotlib 中的 x 轴,以便没有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆