设置X和Y限制在我想重新创建的数字中无法工作

发布于 2025-02-01 10:51:16 字数 4163 浏览 2 评论 0 原文

我想成为这样的人物

​我有下面的代码,

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文件点和钻石来自另一个数据集

任何帮助都将不胜感激!谢谢!

I want to make a figure like this

enter image description here

The problem is that when I set xticks and yticks nothing happens. I have the code below

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)



This is the resulting figure I get. The points are close by since the axis limits have not been set for some reason to the ones in the figure above.

PS: A_ice and H_ice is my own data point while the csv files points and diamonds are from another dataset

enter image description here

Any help will be much appreciated! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔日梦未散 2025-02-08 10:51:16

看起来您正在调用.show(),然后设置tick。这可能是一个问题。

您真正想要的是 > 和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

Looks like you are calling .show() and then setting the ticks. That could be a problem.

What you really want is matplotlib.pyplot.ylim and xlim. That will control the min and max values of the axes.

The end of the code would look more like:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文