【问题标题】:Start x-axis ticks from origin [duplicate]从原点开始 x 轴刻度[重复]
【发布时间】:2021-11-26 10:32:45
【问题描述】:

有多个地块堆叠在一起。每个图的 x 轴刻度从 184.0 开始 是否有任何可能的方法可以从值为 184.0 的原点开始我的 x 刻度?

我的情节是:

#create a figure and edit size
fig=plt.figure(figsize=(20,17))

#define subplots and define their position
plt1=fig.add_subplot(611)
plt2=fig.add_subplot(612)
plt3=fig.add_subplot(613)
plt4=fig.add_subplot(614)
plt5=fig.add_subplot(615)
plt6=fig.add_subplot(616)


plt1.plot("DOY", "By", data=df,color='black')
plt1.set_ylabel("By",size=16)
plt1.set_title("3-6 July 2003",size=20)
plt1.get_yaxis().set_label_coords(-0.05,0.5)

plt2.plot("DOY", "Bz", data=df,color='black')
plt2.set_ylabel("Bz",size=16)
plt2.get_yaxis().set_label_coords(-0.05,0.5)

plt3.plot("DOY", "Vsw", data=df,color='black')
plt3.set_ylabel("Vsw",size=16)
plt3.get_yaxis().set_label_coords(-0.05,0.5)

plt4.plot("DOY", "Nsw", data=df,color='black')
plt4.set_ylabel("Nsw",size=16)
plt4.get_yaxis().set_label_coords(-0.05,0.5)

plt5.plot("DOY", "reconnection_rate", data=df,color='black')
plt5.set_ylabel("MRR",size=16)
plt5.get_yaxis().set_label_coords(-0.05,0.5)

plt6.plot("DOY", "magnetopause_distance", data=df,color='black')
plt6.set_ylabel("MD",size=16)
plt6.set_xlabel("Day of Year",size=16)
plt6.get_yaxis().set_label_coords(-0.05,0.5)

#plt.subplots_adjust(hspace =  ,wspace = 5)
#saving plot in .jpg format
plt.savefig('3 to 6 July 2003 plot.jpg', format='jpeg',dpi=None, edgecolor='black', transparent=True, bbox_inches='tight')

更新: 尝试后plt.margins(x=0) 我得到这样的东西

【问题讨论】:

  • from origin ?从左下角?
  • 是从左角。我希望 0 和 184.0 重合。

标签: python pandas matplotlib plot


【解决方案1】:

简答:对于您的每个plt1plt2...(实际上是轴),请致电plt<i>.set_xlim(184)


一般来说,最好提供一个运行示例,这是从您的代码示例中得出的简短说明:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

N = 100

df = pd.DataFrame(dict(
    DOY=np.linspace(184, 188, num=N),
    By=np.random.randn(N).cumsum(),
    Bz=np.random.randn(N).cumsum(),
))

fig = plt.figure()

ax1 = fig.add_subplot(611)
ax2 = fig.add_subplot(612)

ax1.plot("DOY", "By", data=df, color="black")
ax2.plot("DOY", "Bz", data=df, color="black")

# This is the added part:
ax1.set_xlim(184)
ax2.set_xlim(184)

plt.tight_layout()
plt.show()

【讨论】:

    【解决方案2】:

    我没有你的 y 轴值,但我想这个答案会让你了解如何解决你的问题。

    import matplotlib.pyplot as plt
    import numpy as np
    
    df = [184, 185, 186, 187, 188]
    
    fig=plt.figure(figsize=(20,17))
    
    plt1=fig.add_subplot(611)
    plt1.plot(data=df,color='black')
    plt1.set_ylabel("By",size=16)
    plt1.set_title("3-6 July 2003",size=20)
    plt1.get_yaxis().set_label_coords(-0.05,0.5)
    plt1.set_xlim([df[0], df[-1]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2012-12-12
      • 2012-12-24
      • 1970-01-01
      相关资源
      最近更新 更多