matplotlib顶部底部不同

时间:2013-11-09 23:16:53

标签: matplotlib

有没有办法让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()

2 个答案:

答案 0 :(得分:7)

您可以使用双轴,然后可以分别设置每个侧面的属性:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0, 1, 3], 'o')

axR = ax.twinx()
axT = ax.twiny()

ax.tick_params(direction = 'out')
axR.tick_params(direction = 'in')

ax.tick_params(direction = 'out')
axT.tick_params(direction = 'in')

plt.show()

enter image description here

答案 1 :(得分:3)

#11859升级到class Item extends StatefulWidget { Item({ Key key, this.items, this.showAd, this.user, this.item, this.favorites, }) : super(key: key); final List<Imodel> items; final bool showAd; final UserModel user; final item; final List<FireFavorites> favorites; @override _ItemState createState() => _ItemState(); } class _ItemState extends State<Item> { DateTime _lastViewed; bool _viewRequest = true; final _adWidget = AdMobWidget(); bool _favoriteItem = false; @override void initState() { super.initState(); final isFavorite = widget.favorites .where((element) => element.id == widget.item.documentID); if (isFavorite.length > 0) { _favoriteItem = true; } } @override Widget build(BuildContext context) { if (widget.showAd) { _adWidget.showAd(); } final _headerStyle = TextStyle(fontWeight: FontWeight.bold); Widget _itemBody(item) { return ListView( children: <Widget>[ Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ ... ], ), ], ); } _future() { return FutureBuilder<DocumentSnapshot>( future: Firestore.instance .collection('content') .document(widget.item.documentID) .get(source: Source.serverAndCache), builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) { List<Widget> children; if (snapshot.hasData) { children = <Widget>[Expanded(child: _itemBody(snapshot.data))]; } else if (snapshot.hasError) { children = <Widget>[ Icon( Icons.error_outline, color: Colors.red, size: 60, ), Padding( padding: const EdgeInsets.only(top: 16), child: Text('Error: ${snapshot.error}'), ) ]; } else { children = <Widget>[ SizedBox( child: CircularProgressIndicator(), width: 60, height: 60, ), const Padding( padding: EdgeInsets.only(top: 16), child: Text('Awaiting result...'), ) ]; } return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: children, ), ); }, ); } return Scaffold( resizeToAvoidBottomPadding: false, backgroundColor: Color(0xff2398C3), appBar: AppBar( elevation: 0, flexibleSpace: Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Color(0xff042C5C), Color(0xff2398C3)], ), ), ), actions: <Widget>[ BookmarkIcon( isFav: _favoriteItem, documentID: widget.item.documentID, userID: widget.user.uid, ), Padding( padding: EdgeInsets.only( right: 20.0, ), child: GestureDetector( onTap: () {}, child: Icon( Icons.share, size: 18.0, ), ), ), ], ), body: Container( width: double.infinity, height: double.infinity, padding: EdgeInsets.only( top: 25.0, left: 30.0, right: 15.0, ), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(32), topRight: Radius.circular(32), ), ), child: widget.item.documentID != null ? _future() : Container(), ), ); } } 之后,我们现在可以使用matplotlib>=3.1.0secondary_xaxis来获得独立的刻度指示:

secondary_yaxis

产生这个数字:

Axis with independent tick directions (in/out/inout) left and right as well as top and bottom.