Matplotlib:在子图

时间:2016-04-20 14:45:28

标签: python matplotlib plot

我想创建一个包含四个子图的图形。连续的每个绘图共享相同的y轴,同一列中的绘图共享相同的x轴。在每个轴上我使用科学记数法。虽然我可以使用ticklabel_format删除刻度数,但这不会删除轴上的指数。使用ax1.xaxis.set_visible(False)时,1e5处的x-axis会被删除,但也会删除刻度线。如何在保留刻度线的同时仅删除与另一个轴共用轴的子图中的1eX?例如,如何摆脱子图2中的1e51e2

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()

ax3 = fig.add_subplot(223)
ax1 = fig.add_subplot(221, sharex = ax3)
ax4 = fig.add_subplot(224, sharey = ax3)
ax2 = fig.add_subplot(222, sharex = ax4, sharey = ax1)

#First plot
x = np.arange(0, 10**5, 100)
y = x
ax1.plot(x,y)
ax1.set_title('Subplot 1')

# Third plot
y = -x
ax3.plot(x,y)
ax3.set_title('Subplot 3')

#Second plot
x = np.arange(0, 100)
y = 10**3 * x + 100
ax2.plot(x,y)
ax2.set_title('Subplot 2')

#Fourth plot
y = -10**3 * x - 100
ax4.plot(x,y)
ax4.set_title('Subplot 4')

ax4.ticklabel_format(style = 'sci', axis='x', scilimits=(0,0))
ax3.ticklabel_format(style = 'sci', axis='x', scilimits=(0,0))
ax1.ticklabel_format(style = 'sci', axis='y', scilimits=(0,0))
ax3.ticklabel_format(style = 'sci', axis='y', scilimits=(0,0))

plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax4.get_yticklabels(), visible=False)

plt.show()

返回:

enter image description here

2 个答案:

答案 0 :(得分:6)

如果为每个轴添加这些线(ax1作为示例):

ax1.xaxis.get_offset_text().set_visible(False)
ax1.yaxis.get_offset_text().set_visible(False)

这将从两个轴上删除科学记数法文本。

答案 1 :(得分:0)

将第一部分修改为:

    protected ElasticClient Client;
    IndexName index = "employee";

    public ElasticSearchRepository(Uri elasticServerUri)
    {
        var connection = new ConnectionSettings(elasticServerUri).DefaultIndex("employee");
        this.Client = new ElasticClient(connection);

    }

    //This is how i create Index
    public void CreateIndex()
    {

        var settings = new IndexSettings();
        settings.NumberOfReplicas = 1;
        settings.NumberOfShards = 1;

        var indexstate = new IndexState();
        indexstate.Settings = settings;


        Client.CreateIndex(index, g => g.Index(index)
              .InitializeUsing(indexstate)
              .Mappings(j => j.Map<Employee>(h => h.AutoMap(1))));

    }

    public List<Employee> Search(string search)
    {
        //All 3 searches are not wokring for me
        var response = Client.Search<Employee>(s => s
                             .AllIndices()
                             .AllTypes()
                             .From(0)
                             .Size(10)
                             .Query(q =>q
                             .Term(t => t.FirstName, "Eldho")));

        var result = Client.Search<Employee>(h => h
                            .Query(q => q
                                .Match(m => m.Field("FirstName").Query(search))));

        var result2 = Client.Search<Employee>(h => h
                     .Type("employee")
                     .Query(k => k
                     .Term(g => g.FirstName, "Eldho")));

        return result.Documents.ToList();
    }

然后添加:

ax3 = fig.add_subplot(223)
ax1 = fig.add_subplot(221, sharex = ax3)
ax4 = fig.add_subplot(224)
ax2 = fig.add_subplot(222, sharex = ax4)