Код: Выделить всё
TypeError: unhashable type: 'Series'
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime
fig, ax = plt.subplots(figsize=(10, 10))
data = {'x': [1, 2, 3,4, 5, 6], 'y': [4, 5, 6, 7, 8, 9],'a': [1, 3, 2, 3, 2, 1], 'b': [0, 0, 0, 0, 0, 0]}
df = pd.DataFrame(data)
#Convert day numbers to dates
#****************************
days = {}
for i in range(0, 6):
days[i] = (datetime.datetime(1999, 10, 1) + datetime.timedelta(days=i)).strftime("%d %b") # get date format
dfd = pd.DataFrame(list(days.items())) #A dataframe of dates
dfmx=df['y'].max()
dfmx = int(dfmx) #cater for dfmx a float
#Specify x, y ticks, labels and titles
#*************************************
plt.xticks(ticks = list(range(0, 6, 1)))
plt.yticks(np.arange(0, dfmx, step=1))
plt.xlabel("Days From 01 Oct")
ax.set_title('Some Title: \n ----------------')
plt.ylabel("Values")
def plot_with_checks(df, x_col, y_col):
"""
Plots if specified columns exist and contain non-null data.
"""
if x_col in df.columns and y_col in df.columns:
if df[x_col].notna().sum() > 0 and df[y_col].notna().sum() > 0:
plt.plot(x=x_col, y=y_col, color='orange', label='y_col')
else:
print(f"Plotting ('{y_col}') skipped: no data.")
#Generate plots
#**************
plot_with_checks(df, dfd[1], 'a')
plot_with_checks(df, dfd[1], 'b')
plt.plot(dfd[1], df['x'], color='b', label='x')
plt.plot(dfd[1], df['y'], color='g', label='y')
plt.legend(loc = 'lower right', bbox_to_anchor=(1.0, 0), fontsize=12)
ax.grid()
plt.savefig('Sample Plot.png')
plt.show()
Код: Выделить всё
TypeError: unhashable type: 'Series'
Код: Выделить всё
if x_col in df.columns and y_col in df.columns:
Подробнее здесь: https://stackoverflow.com/questions/798 ... ython-plot