我有一个CSV文件e:\dir1\datafile.csv.它包含三列,需要跳过10个标题和尾随行.我想用numpy.loadtxt()绘制它,我没有找到任何严格的文档.
以下是我从网上发现的几次尝试开始写的内容.
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
def read_datafile(file_name):
# the skiprows keyword is for heading, but I don't know if trailing lines
# can be specified
data = np.loadtxt(file_name, delimiter=',', skiprows=10)
return data
data = read_datafile('e:\dir1\datafile.csv')
x = ???
y = ???
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_title("Mains power stability")
ax1.set_xlabel('time')
ax1.set_ylabel('Mains voltage')
ax1.plot(x,y, c='r', label='the data')
leg = ax1.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud) 是否有rcParams['legend.frameon'] = 'False'一种简单的方法用给定的颜色填充图例区域背景.更具体地说,我希望在图例区域看不到网格,因为它会扰乱文本阅读.
关键字framealpha听起来像我需要的但它不会改变任何东西.
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['legend.frameon'] = 'False'
plt.plot(range(5), label = u"line")
plt.grid(True)
plt.legend(loc = best)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
legend = plt.legend(frameon = 1)
frame = legend.get_frame()
frame.set_color('white')
Run Code Online (Sandbox Code Playgroud)
但是我需要问一下如何在保持框架的同时改变背景颜色?有时我想用白色以外的背景颜色打开它.还有,有没有办法改变框架的颜色?使用上面的代码我期望只改变帧的颜色,而不是背景.
有没有办法增加matplotlib中的刻度的大小和大小,而不必像这样写一长段代码:
for line in ax1.yaxis.get_ticklines():
line.set_markersize(25)
line.set_markeredgewidth(3)
Run Code Online (Sandbox Code Playgroud)
这段代码的问题在于它使用的循环通常需要大量的CPU使用.
在matplotlib中是否有一种方法可以在标签和轴之间设置刻度,因为默认情况下它在Origin中?在线示例部分未显示具有此功能的单个图.我喜欢在绘图区域外有蜱虫,因为有时情节会在内部隐藏蜱虫.
有没有办法让matplotlib图中的顶部刻度和底部勾选?有时我有数据隐藏标记,我想只为受影响的一方设置标记.
以下代码将同时影响顶部和底部或左右两侧.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot( 111 )
ax.plot( [0, 1, 3], 'o' )
ax.tick_params( direction = 'out' )
plt.show()
Run Code Online (Sandbox Code Playgroud) 我想制作一个简单的箭头和一个双头箭头.我使用以下方法制作一个简单的箭头,但我怀疑这是最简单的方法:
import matplotlib.pyplot as plt
arr_width = .009 # I don't know what unit it is here.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(range(10))
ax1.arrow(1, 1, 0, .5, width = arr_width, head_width = 3 * arr_width,
head_length = 9 * arr_width)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我找不到如何用这种方法制作两个头箭.
我将matplotlib设置为在绘图区域外放置刻度,但现在它们在相应的标签上重叠.tick_params方法不提供任何设置相应标签位置的选项.所以我想我将不得不使用text()方法编写自己的函数.同时有人有更好的建议吗?
我正在编写一个函数来修改图形上的轴大小和位置,但是当双轴出现时会出现问题:
import matplotlib.pyplot as plt
def fig_layout(fig, vspace = 0.3): # function to make space at the bottom for legend box and
#+ other text input
for ax in ~~~fig.axes~~~: # Here 'fig.axes' is not right, I need to find the exact syntax
#+ I need to put
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * vspace,
box.width, box.height * (1 - vspace)])
x = np.arange(10)
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
n = 3
line = {} …Run Code Online (Sandbox Code Playgroud) 我想定位一个带有关键字参数的文本框,例如与图例“loc”选项一起使用的关键字参数,即“左上角”、“右上角”、“右下角”、“左下角”。基本目的是将文本框与图例对齐。我在这里找到了一个建议:在 matplotlib 中自动定位文本框,但它仍然使用我必须玩的坐标来获得我想要的东西,特别是如果我想根据文本的长度将它放在绘图区域的右侧放在盒子里。除非我可以将文本框的右角之一设置为坐标参考。
a = np.array([1, 2, 3])
aa = np.array([1], [2], [3])
b = np.array([1, 2, 3])
bb = np.array([1], [2], [3])
np.concatenate((a, b), axis = 1)
array([1, 2, 3, 1, 2, 3]) # It's ok, that's what I was expecting
np.concatenate((a, b), axis = 0)
array([1, 2, 3, 1, 2, 3]) # It's not ok, that's not what I was expecting
Run Code Online (Sandbox Code Playgroud)
我在期待:
array([[1, 2, 3],
[1, 2, 3]])
Run Code Online (Sandbox Code Playgroud)
即使有aa和bb,我也会得到同样的不一致.那么是否有一个简单的解决方案可以沿轴0连接两个一维数组?