【问题标题】:matplotlib - could not convert string to floatmatplotlib - 无法将字符串转换为浮点数
【发布时间】:2018-02-28 11:45:32
【问题描述】:

我很难理解为什么 matplotlib 要将字符串转换为我的 x 轴的浮点数。

x 值(月/日)

x = ['Jul 01', 'Jul 02', 'Jul 03', 'Jul 04', 'Jul 05', 'Jul 06', 'Jul 07', 'Jul 08', 'Jul 09', 'Jul 10', 'Jul 11', 'Jul 12', 'Jul 13', 'Jul 14', 'Jul 15', 'Jul 16', 'Jul 17', 'Jul 18', 'Jul 19', 'Jul 20', 'Jul 21', 'Jul 22', 'Jul 23', 'Jul 24', 'Jul 25', 'Jul 26', 'Jul 27', 'Jul 28', 'Jul 29', 'Jul 30', 'Jul 31', 'Aug 01', 'Aug 02', 'Aug 03', 'Aug 04', 'Aug 05', 'Aug 06', 'Aug 07', 'Aug 08', 'Aug 09', 'Aug 10', 'Aug 11', 'Aug 12', 'Aug 13', 'Aug 14', 'Aug 15', 'Aug 16', 'Aug 17', 'Aug 18', 'Aug 19', 'Aug 20', 'Aug 21', 'Aug 22', 'Aug 23', 'Aug 24', 'Aug 25', 'Aug 26', 'Aug 27', 'Aug 28', 'Aug 29', 'Aug 30', 'Aug 31', 'Sep 01', 'Sep 02', 'Sep 03', 'Sep 04', 'Sep 05', 'Sep 06', 'Sep 07', 'Sep 08', 'Sep 09', 'Sep 10', 'Sep 11', 'Sep 12', 'Sep 13', 'Sep 14', 'Sep 15', 'Sep 16', 'Sep 17', 'Sep 18', 'Sep 19', 'Sep 20', 'Sep 21', 'Sep 22', 'Sep 23', 'Sep 24', 'Sep 25', 'Sep 26', 'Sep 27', 'Sep 28', 'Sep 29', 'Sep 30']

y 值(平均温度)

y = [80.5, 81.5, 78.0, 69.5, 72.0, 75.0, 81.0, 81.0, 79.5, 77.5, 78.5, 78.0, 77.5, 78.0, 79.0, 74.5, 74.0, 72.5, 72.0, 73.0, 76.0, 78.5, 80.0, 75.0, 74.0, 75.0, 78.0, 75.0, 70.0, 71.5, 75.0, 77.5, 68.5, 71.0, 77.0, 80.5, 76.5, 74.5, 74.0, 76.5, 78.0, 79.0, 74.5, 75.0, 70.0, 67.0, 70.5, 74.0, 72.0, 73.0, 77.0, 74.0, 72.0, 72.0, 72.0, 76.0, 79.5, 80.0, 74.0, 70.5, 72.5, 81.5, 81.5, 84.5, 79.0, 78.0, 79.5, 79.0, 73.0, 70.0, 68.0, 71.5, 76.0, 70.0, 63.5, 62.0, 63.0, 64.0, 64.0, 66.5, 60.0, 66.0, 71.0, 63.0, 61.5, 64.5, 60.5, 67.5, 71.5, 74.0, 73.0, 66.5]

假设我做了一个非常简单的情节

plt.plot(x,y)

我收到错误:ValueError:无法将字符串转换为浮点数“Jul 01”。

此数据是从数据框中提取的。如果我尝试使用数据框进行绘图,我能够成功地创建绘图。但是,我想在一个图上覆盖多个数据集 - 所以我希望能够使用不同的数据集进行多个 plt.plot 调用。

干杯

【问题讨论】:

  • 不确定您要在这里解决哪些问题。您想将不同的数据框绘制到同一个图上,还是想让plt.plot 处理您的数据?

标签: python datetime matplotlib


【解决方案1】:

下面显示了如何将多组数据绘制到同一个图表上。您需要将日期值转换为适合 matplotlib 的形式。首先将值转换为datetime 对象,然后使用date2num() 函数。最后,可以使用月份定位器格式化 x 轴。

from datetime import datetime
import matplotlib.pyplot as plt            
from matplotlib import dates, pyplot

x2 = ['Jan 01', 'Jan 02', 'Jan 03', 'Jan 04', 'Jan 05', 'Jan 06', 'Jan 07', 'Jan 08', 'Jan 09', 'Jan 10', 'Jan 11', 'Jan 12', 'Jan 13', 'Jan 14', 'Jan 15', 'Jan 16', 'Jan 17', 'Jan 18', 'Jan 19', 'Jan 20', 'Jan 21', 'Jan 22', 'Jan 23', 'Jan 24', 'Jan 25', 'Jan 26', 'Jan 27', 'Jan 28', 'Jan 29', 'Jan 30', 'Jan 31', 'Feb 01', 'Feb 02', 'Feb 03', 'Feb 04', 'Feb 05', 'Feb 06', 'Feb 07', 'Feb 08', 'Feb 09', 'Feb 10', 'Feb 11', 'Feb 12', 'Feb 13', 'Feb 14', 'Feb 15', 'Feb 16', 'Feb 17', 'Feb 18', 'Feb 19', 'Feb 20', 'Feb 21', 'Feb 22', 'Feb 23', 'Feb 24', 'Feb 25', 'Feb 26', 'Feb 27', 'Feb 28', 'Mar 01', 'Mar 02', 'Mar 03', 'Mar 04', 'Mar 05', 'Mar 06', 'Mar 07', 'Mar 08', 'Mar 09', 'Mar 10', 'Mar 11', 'Mar 12', 'Mar 13', 'Mar 14', 'Mar 15', 'Mar 16', 'Mar 17', 'Mar 18', 'Mar 19', 'Mar 20', 'Mar 21', 'Mar 22', 'Mar 23', 'Mar 24', 'Mar 25', 'Mar 26', 'Mar 27', 'Mar 28', 'Mar 29', 'Mar 30']
y2 = [80.5, 81.5, 78.0, 69.5, 72.0, 75.0, 81.0, 81.0, 79.5, 77.5, 78.5, 78.0, 77.5, 78.0, 79.0, 74.5, 74.0, 72.5, 72.0, 73.0, 76.0, 74.0, 75.0, 78.0, 75.0, 70.0, 71.5, 75.0, 77.5, 68.5, 71.0, 77.0, 80.5, 76.5, 74.5, 74.0, 76.5, 78.0, 79.0, 74.5, 75.0, 70.0, 67.0, 70.5, 74.0, 72.0, 73.0, 77.0, 74.0, 72.0, 72.0, 72.0, 76.0, 79.5, 80.0, 74.0, 70.5, 72.5, 81.5, 81.5, 84.5, 79.0, 78.0, 79.5, 79.0, 73.0, 70.0, 68.0, 71.5, 76.0, 70.0, 63.5, 62.0, 63.0, 64.0, 64.0, 66.5, 60.0, 66.0, 71.0, 63.0, 61.5, 64.5, 60.5, 67.5, 71.5, 74.0, 73.0, 66.5]

x1 = ['Jul 01', 'Jul 02', 'Jul 03', 'Jul 04', 'Jul 05', 'Jul 06', 'Jul 07', 'Jul 08', 'Jul 09', 'Jul 10', 'Jul 11', 'Jul 12', 'Jul 13', 'Jul 14', 'Jul 15', 'Jul 16', 'Jul 17', 'Jul 18', 'Jul 19', 'Jul 20', 'Jul 21', 'Jul 22', 'Jul 23', 'Jul 24', 'Jul 25', 'Jul 26', 'Jul 27', 'Jul 28', 'Jul 29', 'Jul 30', 'Jul 31', 'Aug 01', 'Aug 02', 'Aug 03', 'Aug 04', 'Aug 05', 'Aug 06', 'Aug 07', 'Aug 08', 'Aug 09', 'Aug 10', 'Aug 11', 'Aug 12', 'Aug 13', 'Aug 14', 'Aug 15', 'Aug 16', 'Aug 17', 'Aug 18', 'Aug 19', 'Aug 20', 'Aug 21', 'Aug 22', 'Aug 23', 'Aug 24', 'Aug 25', 'Aug 26', 'Aug 27', 'Aug 28', 'Aug 29', 'Aug 30', 'Aug 31', 'Sep 01', 'Sep 02', 'Sep 03', 'Sep 04', 'Sep 05', 'Sep 06', 'Sep 07', 'Sep 08', 'Sep 09', 'Sep 10', 'Sep 11', 'Sep 12', 'Sep 13', 'Sep 14', 'Sep 15', 'Sep 16', 'Sep 17', 'Sep 18', 'Sep 19', 'Sep 20', 'Sep 21', 'Sep 22', 'Sep 23', 'Sep 24', 'Sep 25', 'Sep 26', 'Sep 27', 'Sep 28', 'Sep 29', 'Sep 30']
y1 = [80.5, 81.5, 78.0, 69.5, 72.0, 75.0, 81.0, 81.0, 79.5, 77.5, 78.5, 78.0, 77.5, 78.0, 79.0, 74.5, 74.0, 72.5, 72.0, 73.0, 76.0, 78.5, 80.0, 75.0, 74.0, 75.0, 78.0, 75.0, 70.0, 71.5, 75.0, 77.5, 68.5, 71.0, 77.0, 80.5, 76.5, 74.5, 74.0, 76.5, 78.0, 79.0, 74.5, 75.0, 70.0, 67.0, 70.5, 74.0, 72.0, 73.0, 77.0, 74.0, 72.0, 72.0, 72.0, 76.0, 79.5, 80.0, 74.0, 70.5, 72.5, 81.5, 81.5, 84.5, 79.0, 78.0, 79.5, 79.0, 73.0, 70.0, 68.0, 71.5, 76.0, 70.0, 63.5, 62.0, 63.0, 64.0, 64.0, 66.5, 60.0, 66.0, 71.0, 63.0, 61.5, 64.5, 60.5, 67.5, 71.5, 74.0, 73.0, 66.5]

ax = pyplot.gca()
hfmt = dates.DateFormatter('%b')
ax.xaxis.set_major_formatter(hfmt)
ax.xaxis.set_major_locator(dates.MonthLocator())

for x, y in [(x1, y1), (x2, y2)]:
    dt_x = dates.date2num([datetime.strptime(d, "%b %d").replace(year=2017) for d in x])
    plt.plot(dt_x, y)

plt.show()

这将显示这两组数据如下:

【讨论】:

  • 按需要工作:)
【解决方案2】:

如果您只想在x 轴上绘制一个字符串列表,您可以这样做:

xn = range(len(x))
plt.plot(xn, y)
plt.xticks(xn, x)

您首先需要将字符串标签映射到整数(xxn),然后使用第二个参数或 xticks 将其设置为字符串值。

如果您有多个数据集,您只需将plt.plot 行与不同的y 循环,并在其后执行plt.xticks

【讨论】:

  • 我认为我的斗争是为什么 matplotlib 尝试将日期转换为浮点数...... matplotlib 是否总是要求您有 x 轴的数字?我不能沿 x 使用字符串吗?
  • 不,你不是。绘图中的点本质上是 X、Y(数字)中的坐标。为了让 matplotlib 知道把东西放在哪里,数字是必需的。 绘制字符串的所有绘图函数在幕后执行数字映射。 @MartinEvans 的回答展示了如何通过格式化轴来实现类似的效果,尤其是几个月。
  • 太棒了 - 我认为这澄清了我的问题。我真的不明白字符串值和 x 值的数字之间需要映射。现在更有意义了。
猜你喜欢
  • 2016-11-29
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多