How to plot the graph in more accurate way?

风格不统一 提交于 2019-12-24 02:08:06

问题


I have a data set that consists of 1440 rows × 297 columns. I tried to plot 03_jan_2018 in terms of Time(X-Axis) and Density (Y-Axis), but I'm faced with a problem. The outcome graph is not enough clear and also the X-Axis is not appeared!!

I would like to make something like this:

But I'm ending up with this:

Can anyone help me?

Thanks in advance!


回答1:


An easy fix for this is to increase the figure size.

plt.figure(figsize=(80,40))
plt.plot(data.hour_formatted, data['03_jan_18'])
plt.show()



回答2:


I would use the plt.xticks(np.arange(0,25,6)) to select just the hours that you want. It looks like it's trying to put in too many of them by default, and really you want just a handful of values (in this case only every 6 hours has a tick). Tune the range value to just give the ones you want.




回答3:


The x-axis has appeared, but it seems that you're facing the same problem as described here. Anyway, I'll show you how you can get what you want and also avoid possible problems with the x-axis notations.

Plot 1:

Code 1:

# imports
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# random data or other data sources
np.random.seed(123)
rows = 1440
df = pd.DataFrame(np.random.uniform(-1,1,size=(rows, 2)),
                  index=pd.date_range('1/1/2000', periods=1440),
                    columns=list('AB'))

df['A'] = df['A'].cumsum()
df['B'] = df['B'].cumsum()

# Plot
fig, ax = plt.subplots()
t = df.index
ax.plot(t, df['A'])
ax.plot(t, df['B'], color='red')

You can also edit and adjust the axis notations like this:

Plot 2:

Code 2:

# imports
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# random data or other data sources
np.random.seed(123)
rows = 1440
df = pd.DataFrame(np.random.uniform(-1,1,size=(rows, 2)),
                  index=pd.date_range('1/1/2020', periods=1440),
                    columns=list('AB'))

df['A'] = df['A'].cumsum()
df['B'] = df['B'].cumsum()

# Make a list of empty myLabels
myLabels = ['']*len(df.index)

# Plot
fig, ax = plt.subplots()
t = df.index
ax.plot(t, df['A'])
ax.plot(t, df['B'], color='red')

# Set labels on every Nth element in myLabels specified by the interval variable
myLabels = ['']*len(df.index)
interval = 2
myLabels[::interval] = [item.strftime('%Y - %m - %d') for item in df.index[::interval]]
ax.xaxis.set_major_formatter(ticker.FixedFormatter(myLabels))
plt.gcf().autofmt_xdate()

# Tilt the labels
plt.setp(ax.get_xticklabels(), rotation=30, fontsize=10)
plt.show()


来源:https://stackoverflow.com/questions/57751132/how-to-plot-the-graph-in-more-accurate-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!