设置 x 和 y 限制在我想重新创建的图形中不起作用
Setting x and y limits not working in a figure I want to recreate
我想做这样的图
问题是当我设置 xticks 和 yticks 时没有任何反应。我有下面的代码
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cmt
import xarray as xr
import matplotlib.ticker as ticker
from shapely import vectorized
import pandas as pd
import csv
# Open file
grl = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/OUTPUTS/output/ens1/0/yelmo1d.nc')
topo = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/Greenland/GRL-16KM/GRL-16KM_TOPO-M17.nc')
#My data
A_ice = grl["A_ice"][0]
V_ice = grl["V_ice"][0]
# Data points from goelzer
points = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv")
diamonds = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv")
#Convert to list of tuples
with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv', newline='') as f:
reader = csv.reader(f)
points = [tuple(row) for row in reader]
with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv', newline='') as f:
reader = csv.reader(f)
diamonds = [tuple(row) for row in reader]
# Now start the plots
# Create a figure
fig = plt.figure()
# Add a subplot
ax = fig.add_subplot()
ax.set_title("Grounded ice area and grounded volume")
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x_mydata = A_ice
y_mydata = V_ice
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid()
plt.plot(x_mydata, y_mydata, marker="^", markersize=20, markeredgecolor="red", markerfacecolor="red")
plt.plot(float(points[0][0]),float(points[0][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[1][0]),float(points[1][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[2][0]),float(points[2][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[3][0]),float(points[3][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[4][0]),float(points[4][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[5][0]),float(points[5][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[6][0]),float(points[6][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[7][0]),float(points[7][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[8][0]),float(points[8][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[9][0]),float(points[9][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[10][0]),float(points[10][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[11][0]),float(points[11][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[12][0]),float(points[12][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[13][0]),float(points[13][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(diamonds[0][0]),float(diamonds[0][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.plot(float(diamonds[1][0]),float(diamonds[1][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.show()
x_axis = np.arange(1.6,2,1)
y_axis = np.arange(1.6,3.3,1)
plt.xticks(x_axis)
plt.xticks(y_axis)
#ax.set_xticks(x_axis)
#ax.set_yticks(y_axis)
这是我得到的结果图。这些点很接近,因为由于某种原因尚未设置上图中的轴限制。
PS: A_ice 和 H_ice 是我自己的数据点,而 csv 文件点和钻石来自另一个数据集
任何帮助将不胜感激!谢谢!
看起来您正在调用 .show() 然后设置刻度。这可能是个问题。
你真正想要的是matplotlib.pyplot.ylim
和xlim。这将控制轴的最小值和最大值。
代码的结尾看起来更像:
#plt.show()
#x_axis = np.arange(1.6,2,1)
#y_axis = np.arange(1.6,3.3,1)
plt.xlim(left=0, right=2)
plt.ylim(top=3.2, bottom=0)
plt.show() # move here
我想做这样的图
问题是当我设置 xticks 和 yticks 时没有任何反应。我有下面的代码
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cmt
import xarray as xr
import matplotlib.ticker as ticker
from shapely import vectorized
import pandas as pd
import csv
# Open file
grl = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/OUTPUTS/output/ens1/0/yelmo1d.nc')
topo = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/Greenland/GRL-16KM/GRL-16KM_TOPO-M17.nc')
#My data
A_ice = grl["A_ice"][0]
V_ice = grl["V_ice"][0]
# Data points from goelzer
points = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv")
diamonds = pd.read_csv("/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv")
#Convert to list of tuples
with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/points.csv', newline='') as f:
reader = csv.reader(f)
points = [tuple(row) for row in reader]
with open('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Figures/diamonds.csv', newline='') as f:
reader = csv.reader(f)
diamonds = [tuple(row) for row in reader]
# Now start the plots
# Create a figure
fig = plt.figure()
# Add a subplot
ax = fig.add_subplot()
ax.set_title("Grounded ice area and grounded volume")
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x_mydata = A_ice
y_mydata = V_ice
plt.xlim(0, 5)
plt.ylim(0, 5)
plt.grid()
plt.plot(x_mydata, y_mydata, marker="^", markersize=20, markeredgecolor="red", markerfacecolor="red")
plt.plot(float(points[0][0]),float(points[0][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[1][0]),float(points[1][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[2][0]),float(points[2][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[3][0]),float(points[3][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[4][0]),float(points[4][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[5][0]),float(points[5][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[6][0]),float(points[6][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[7][0]),float(points[7][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[8][0]),float(points[8][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[9][0]),float(points[9][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[10][0]),float(points[10][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[11][0]),float(points[11][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[12][0]),float(points[12][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(points[13][0]),float(points[13][1]), marker="o", markersize=14, markeredgecolor="blue", markerfacecolor="blue")
plt.plot(float(diamonds[0][0]),float(diamonds[0][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.plot(float(diamonds[1][0]),float(diamonds[1][1]), marker="D", markersize=14, markeredgecolor="green", markerfacecolor="green")
plt.show()
x_axis = np.arange(1.6,2,1)
y_axis = np.arange(1.6,3.3,1)
plt.xticks(x_axis)
plt.xticks(y_axis)
#ax.set_xticks(x_axis)
#ax.set_yticks(y_axis)
这是我得到的结果图。这些点很接近,因为由于某种原因尚未设置上图中的轴限制。
PS: A_ice 和 H_ice 是我自己的数据点,而 csv 文件点和钻石来自另一个数据集
任何帮助将不胜感激!谢谢!
看起来您正在调用 .show() 然后设置刻度。这可能是个问题。
你真正想要的是matplotlib.pyplot.ylim
和xlim。这将控制轴的最小值和最大值。
代码的结尾看起来更像:
#plt.show()
#x_axis = np.arange(1.6,2,1)
#y_axis = np.arange(1.6,3.3,1)
plt.xlim(left=0, right=2)
plt.ylim(top=3.2, bottom=0)
plt.show() # move here