【发布时间】:2021-10-17 02:28:05
【问题描述】:
我想在一张图表中显示多个数据集。
但我似乎无法让 y 轴工作并得到以下错误:ValueError: x and y must have same first dimension, but have shapes (2,) and (6060000,)
由于我还是一个初学者,并且我从不同的来源复制了部分代码,我的代码很可能在某些地方很糟糕。
我从未问过任何 pandas/matplotlib 问题,所以我希望这是可重现的。
数据框有很多列,但代码示例中只提供了一小部分。
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
channel_data = pd.DataFrame({'Creation date': ['2014-01-02', '2013-09-11', '2007-08-19'], 'Subscriber count': [6060000, 4110000, 4440000 ]})
# get x and y from first channel
now = str(dt.datetime.now())
now = now[:10]
dates = [channel_data["Creation date"][0], now]
dates2 = [channel_data["Creation date"][1], now]
dates3 = [channel_data["Creation date"][2], now]
x1 = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates]
x2 = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates2]
x3 = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in dates3]
# PROBLEM HERE
y1 = range(len(x1)) # i got the x axis to work but am having problems with this part
y2 = range(len(x2))
y3 = range(len(x3))
#y1 = range(0, channel_data["Subscriber count"][0])
# this was my idea of displaying the data (y-axis)
# -----------------------------------------------------------
plt.figure(figsize=(10, 5))
plt.title("Channel growth over time [USD]", fontdict={"fontweight": "bold"})
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.plot(x1, y1, "b.-", label="Carwow") #b.- to choose color=blue, pointer=. , line=normal line
plt.plot(x2, y2, "r.-", label="Doug Demuro")
plt.plot(x3, y3, "g.-", label="Scotty Kilmer")
plt.xlabel("Date", fontdict={"fontsize": 13})
plt.ylabel("Subscribers", fontdict={"fontsize": 12})
plt.legend()
plt.show()
第一张图片显示了当前图表(y 值错误)。 第二张图片显示了我想要如何显示数据的示意图。
我知道这一次有很多问题要问,但也许只是有一个想法或一个我可以去的方向。尝试了很多东西,但没有任何效果。
感谢您的阅读。
【问题讨论】:
标签: python pandas matplotlib plot