添加文本到tricontourf

huangapple go评论101阅读模式
英文:

Adding text to tricontourf

问题

要在使用以下代码制作的tricontoruf绘图中添加文本标题:

  1. import matplotlib.pyplot as plt
  2. from matplotlib import cm
  3. import pandas as pd
  4. dx = {'1': 1, '2': -1, '3': -1, '4': 1, '5': 0}
  5. x = pd.Series(data=dx, index=['1', '2', '3', '4', '5'])
  6. dy = {'1': 1, '2': 1, '3': -1, '4': -1, '5': 0}
  7. y = pd.Series(data=dy, index=['1', '2', '3', '4', '5'])
  8. dT = {'1': 10, '2': 20, '3': 30, '4': 40, '5': 50}
  9. T = pd.Series(data=dT, index=['1', '2', '3', '4', '5'])
  10. plt.figure(figsize=(10,8))
  11. CS=plt.tricontourf(x, y, T, cmap=cm.turbo, extend='neither', levels=np.arange(0,101,5))
  12. plt.colorbar(CS)
  13. # 添加文本标签
  14. plt.text(1, 1, "我的文本", fontsize=12)
  15. plt.show()

将上述plt.text行添加到您的代码中,它将在图上添加文本标签"我的文本",并且您可以根据需要调整文本的位置和字体大小。

英文:

I need to add a text caption to a plot that I make using tricontoruf using the below code:

  1. import matplotlib.pyplot as plt
  2. from matplotlib import cm
  3. import pandas as pd
  4. dx = {'1': 1, '2': -1, '3': -1, '4': 1, '5': 0}
  5. x = pd.Series(data=dx, index=['1', '2', '3', '4', '5'])
  6. dy = {'1': 1, '2': 1, '3': -1, '4': -1, '5': 0}
  7. y = pd.Series(data=dy, index=['1', '2', '3', '4', '5'])
  8. dT = {'1': 10, '2': 20, '3': 30, '4': 40, '5': 50}
  9. T = pd.Series(data=dT, index=['1', '2', '3', '4', '5'])
  10. plt.figure(figsize=(10,8))
  11. CS=plt.tricontourf(x, y, T, cmap=cm.turbo, extend='neither', levels=np.arange(0,101,5))
  12. plt.colorbar(CS)
  13. plt.show()

Want to add something like text(1, 1, "my text", fontsize=12). Can somebody help, please?

答案1

得分: 1

我会使用面向对象的编程方法,然后调用 Axes.text

Axes.text(x, y, s, fontdict=None, **kwargs)

在数据坐标中的位置 x、y 处将文本 s 添加到 Axes 中。

来源 : [matplotlib]

  1. fig, ax = plt.subplots(figsize=(10, 8))
  2. CS = ax.tricontourf(x, y, T, cmap=cm.turbo, extend='neither', levels=np.arange(0, 101, 5))
  3. fig.colorbar(CS), ax.set_aspect('equal')
  4. ax.text(0.5, 0.5, # X & Y 坐标
  5. s='StackOverflow', # <-- 在这里放置您的文本
  6. fontsize=12, # 调整字体大小
  7. ha='center', transform=ax.transAxes)
  8. plt.show()

添加文本到tricontourf

英文:

I would use the OOP approach and then call Axes.text :

> Axes.text(x, y, s, fontdict=None, **kwargs)
>
> Add the text s to the Axes at location x, y in data coordinates.
>
> Source : [matplotlib]

  1. fig, ax = plt.subplots(figsize=(10, 8))
  2. CS = ax.tricontourf(x, y, T, cmap=cm.turbo, extend=&#39;neither&#39;, levels=np.arange(0, 101, 5))
  3. fig.colorbar(CS), ax.set_aspect(&#39;equal&#39;)
  4. ax.text(0.5, 0.5, # X &amp; Y coordinates
  5. s=&#39;StackOverflow&#39;, # &lt;-- put your text here
  6. fontsize=12, # adjust the fontsize
  7. ha=&#39;center&#39;, transform=ax.transAxes)
  8. plt.show()

添加文本到tricontourf

huangapple
  • 本文由 发表于 2023年4月7日 02:25:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定